之前总结过 SpringMVC 的mvc:resources使用映射路径展示文件服务器上的图片 后来发现也可以采用如下的方式进行。
<img src="${rootPath}/getPicture.do?picId=${id}">
@RequestMapping(value = "/getPicture.do")
public void loginCheck(HttpServletRequest request,HttpServletResponse response){
String picId = request.getParameter("picId");
String filePath = xxService.getPicPath(picId);
File file = new File(filePath);
if(file.exists()){
InputStream is = null;
OutputStream out = null;
try{
is = new BufferedInputStream(new FileInputStream(file));
out = response.getOutputStream();
response.setContentType("application/x-jpg");
byte[] buff = new byte[1024];
int length = 0;
while ((length = is.read(buff)) > 0) {
out.write(buff, 0, length);
}
out.flush();
out.close();
is.close();
}catch(Exception e){
System.out.println(e);
}
}
}