码迷,mamicode.com
首页 > 其他好文 > 详细

后台返回输出流的方式显示图片

时间:2019-10-25 18:21:53      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:app   com   read   let   方式   tps   etc   输出流   highlight   

前端请求路径:

<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();
}

 

后台返回输出流的方式显示图片

标签:app   com   read   let   方式   tps   etc   输出流   highlight   

原文地址:https://www.cnblogs.com/lazyli/p/11739245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!