[转]springMVC下的通过<img src="">标签的src访问图片

原文链接:http://blog.csdn.net/kouwoo/article/details/50015693


图片的回显,在src中放入图片的回显路径

[java]  view plain  copy
  1. /** 
  2.      * 百度ueditor编辑器的图片回显   通过<img/>标签中的src访问  例:http://localhost:8080/qxzst-sp-apis//pc/image/getUeditorImg/1480504795791062668/png 
  3.      * @param imgname  图片名 
  4.      * @param imgType   扩展名 
  5.      * @param response 
  6.      */  
  7.     @RequestMapping(value = "getUeditorImg/{imgname}/{imgType}", method = RequestMethod.GET)  
  8.     public void getUeditorImg(@PathVariable String imgname, @PathVariable String imgType,  
  9.             HttpServletResponse response) {  
  10.         if ("null".equals(imgname) || "null".equals(imgType) || StringUtils.isBlank(imgname)  
  11.                 || StringUtils.isNotBlank(imgType)) {  
  12.             return;  
  13.         }  
  14.         BufferedInputStream in = null;  
  15.         BufferedOutputStream out = null;  
  16.         try {  
  17.             DateFormat df = new SimpleDateFormat("yyyyMMdd");  
  18.             String timeStr = imgname.substring(0, imgname.length() - 6);  
  19.             String dateFile = df.format(new Date(Long.parseLong(timeStr)));  
  20.             File file = new File(UEDITOR_IMG_PATH + "/" + dateFile + "/" + imgname + "." + imgType);  
  21.             in = new BufferedInputStream(new FileInputStream(file));  
  22.             out = new BufferedOutputStream(response.getOutputStream());  
  23.             response.setContentType(new MimetypesFileTypeMap().getContentType(file));// 设置response内容的类型  
  24.             response.setHeader("Content-disposition""attachment;filename=" + imgname + "." + imgType);// 设置头部信息  
  25.             byte[] buffer = new byte[10240];  
  26.             int length = 0;  
  27.             while ((length = in.read(buffer)) > 0) {  
  28.                 out.write(buffer, 0, length);  
  29.             }  
  30.             out.flush();  
  31.         } catch (IOException e) {  
  32.             getLogger().error(e.getMessage(), e);  
  33.             writeError500(response, e);  
  34.         } finally {  
  35.             try {  
  36.                 if (in != null) {  
  37.                     in.close();  
  38.                 }  
  39.                 if (out != null) {  
  40.                     out.close();  
  41.                 }  
  42.             } catch (IOException e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.         }  
  46.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值