以流的形式显示图片主要把图片转换成字符流然后传入ServletOutputStream
java代码
public static void writeImage(String path, HttpServletResponse response) throws IOException {
File file = new File(path);
if (!file.exists())
return;
BufferedInputStream buf = null;
ServletOutputStream out = null;
try{
buf = new BufferedInputStream(new FileInputStream(file));
out = response.getOutputStream();
byte[] buffer = new byte[10000];
while(buf.read(buffer) != -1){
out.write(buffer);
}
out.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
if(null != buf)
buf.close();
if(null != out)
out.close();
}
}
html代码
<img src="<%=request.getContextPath%>/user/image.do"/>