后台返回输出流的方式显示图片
Posted lazyli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后台返回输出流的方式显示图片相关的知识,希望对你有一定的参考价值。
前端请求路径:
<img src="<%=basePath%>commericalPart/readPartImage">
后台:
/**
* 读取图片
*/
@RequestMapping("/readPartImage")
@ResponseBody
public void readPartImage(HttpServletRequest request, HttpServletResponse response)throws Exception{
String path = request.getParameter("url");
File file = new File("E:/lazyli/image/part/本田/车身/1230474 防飞溅-17.jpg");
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[is.available()];
is.read(bytes);
is.close();
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = response.getOutputStream();
/*int ch = 0;
while (-1 != (ch = is.read())){
os.write(bytes,0,ch);
}*/
response.setContentType("image/*");
os.write(bytes);
os.close();
}
第二种方式
/**
* 读取图片
*/
@RequestMapping("/readPartImage")
@ResponseBody
public void readPartImage(HttpServletRequest request, HttpServletResponse response)throws Exception{
String path = request.getParameter("url");
File file = new File("E:/lazyli/image/part/本田/车身/1230474 防飞溅-17.jpg");
InputStream is = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = response.getOutputStream();
int ch = 0;
while (-1 != (ch = is.read())){
baos.write(ch);
}
os.write(baos.toByteArray());
is.close();
baos.close();
os.close();
}
以上是关于后台返回输出流的方式显示图片的主要内容,如果未能解决你的问题,请参考以下文章
java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpgpng等结尾的图片)