首页 > 代码库 > java 通过流的方式读取本地图片并显示在jsp 页面上

java 通过流的方式读取本地图片并显示在jsp 页面上

项目中Java代码如下:

@RequestMapping("readImage")
public void readImage(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{
    String magazineId = request.getParameter("magazineId");
    String sql = " SELECT save_path,save_name,extention FROM ta_horizon_info WHERE object_id = ‘" + magazineId + "‘ ";
    Map<String, Object> map = Access.getSingleMap(sql, null);
    String savePath = (String) map.get("save_path");
    String saveName = (String) map.get("save_name");
    String extention = (String) map.get("extention");
    String filePath = savePath + "/" +saveName + "." + extention;
    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();  
    }
}

jsp页面代码:

<img style="width:100%; height:490px" src="<%=request.getContextPath() %>/horizon/module/magazine/readImage.wf?magazineId=${magazine.id }">

效果图:

技术分享

 

java 通过流的方式读取本地图片并显示在jsp 页面上