img远程读取图片

1.前端jsp页面

<!--${item }是从后台传来的图片url-->
"cursor:pointer;"src="${ctx}/product/readImage?filePath=${item }">

2.后台controller

@RequestMapping("/readImage")
public voidreadImage(Modelmodel,HttpServletRequest   request, HttpServletResponse response) throws IOException{
        String filePath = request.getParameter("filePath");
        File filePic = new File(filePath);
        if(filePic.exists()){
           FileInputStream is = new FileInputStream(filePic);
           int i = is.available(); // 得到文件大小  
           byte data[] = new byte[i];  
           is.read(data); // 读数据  
           is.close();  
           response.setContentType("image/*"); // 设置返回的文件类型
           // 得到向客户端输出二进制数据的对象  
           OutputStream toClient = response.getOutputStream(); 
           toClient.write(data); // 输出数据  
           toClient.close();  
        }
    }

你可能感兴趣的:(jave-web,前端,jsp)