首页 > 编程语言 > 详细

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

时间:2017-03-17 19:20:43      阅读:599      评论:0      收藏:0      [点我收藏+]

项目中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 页面上

原文:http://www.cnblogs.com/henuyuxiang/p/6567826.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!