<img src="/loadImg?path='xxx'" />
/**
* IO流读取存在服务器上的图片
*
* @return
* @throws IOException
*/
@RequestMapping(value = "/loadImg", method = RequestMethod.GET)
public void loadImg(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
//这里省略掉通过id去读取图片的步骤。
File file = new File(path);//imgPath为服务器图片地址
if (file.exists() && file.isFile()) {
FileInputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];// 一次读取1k
while ((count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}