码迷,mamicode.com
首页 > 编程语言 > 详细

java文件下载

时间:2021-05-24 00:28:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:addheader   nal   charset   exist   打印   put   mesh   系统   rgb   

@RequestMapping(value = "/downfile", method = RequestMethod.GET)
    public void downfile(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fileName = request.getParameter("fileName");
            String newPath = request.getSession().getServletContext().getRealPath("") + File.separator
                     + fileName;
            LoggerUtil.info(this.getClass(),"【获取到打印模板】:"+newPath);
            File file = new File(newPath);
            if (!file.exists()) {
                response.getWriter().write("<font color=‘red‘>系统打印异常,请联系管理员</font>");
                return;
            }
            String fileNameShow = fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
            FileInputStream is = new FileInputStream(file);
            // 设置相应的头部
            response.addHeader("pragma", "NO-cache");
            response.addHeader("Cache-Control", "no-cache");
            response.addDateHeader("Expries", 0);
            response.setContentType("application/x-download;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileNameShow, "UTF-8"));
            IOUtils.copy(is, response.getOutputStream());
            is.close();
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "模板打印异常" + e, e);
            try {
                response.getWriter().write("<font color=‘red‘>系统打印异常,请联系管理员</font>");
            } catch (IOException e1) {
                LoggerUtil.error(this.getClass(), "response写出异常" + e1, e1);
            }
        }finally {
            
        }
    }

 

java文件下载

标签:addheader   nal   charset   exist   打印   put   mesh   系统   rgb   

原文地址:https://www.cnblogs.com/qiwb/p/14738829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!