为了账号安全,请及时绑定邮箱和手机立即绑定

基于Springmvc的文件下载

基于Springmvc的文件下载

那个怪蜀黍是谁额 2018-10-20 11:44:42
@RequestMapping(value = "getUeditorImg/{imgname}/{imgType}", method = RequestMethod.GET)       public void getUeditorImg(@PathVariable String imgname, @PathVariable String imgType,               HttpServletResponse response) {           if ("null".equals(imgname) || "null".equals(imgType) || StringUtils.isBlank(imgname)                   || StringUtils.isNotBlank(imgType)) {               return;           }           BufferedInputStream in = null;           BufferedOutputStream out = null;           try {               DateFormat df = new SimpleDateFormat("yyyyMMdd");               String timeStr = imgname.substring(0, imgname.length() - 6);               String dateFile = df.format(new Date(Long.parseLong(timeStr)));               File file = new File(UEDITOR_IMG_PATH + "/" + dateFile + "/" + imgname + "." + imgType);               in = new BufferedInputStream(new FileInputStream(file));               out = new BufferedOutputStream(response.getOutputStream());               response.setContentType(new MimetypesFileTypeMap().getContentType(file));// 设置response内容的类型               response.setHeader("Content-disposition", "attachment;filename=" + imgname + "." + imgType);// 设置头部信息               byte[] buffer = new byte[10240];               int length = 0;               while ((length = in.read(buffer)) > 0) {                   out.write(buffer, 0, length);               }               out.flush();           } catch (IOException e) {               getLogger().error(e.getMessage(), e);               writeError500(response, e);           } finally {               try {                   if (in != null) {                       in.close();                   }                   if (out != null) {                       out.close();                   }               } catch (IOException e) {                   e.printStackTrace();               }           }       }
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 8372 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信