zoukankan      html  css  js  c++  java
  • 14 springboot文件下载

    文件下载特别简单,给个文件路径,输出个流即可。

    controller

        @GetMapping(value = "/download")
        @IgnoreUserToken
        public void downLoad(String id, HttpServletResponse response){
            Template template = templateBiz.selectById(id);
            String templatePath = template.getTemplatePath();
            String templateName = template.getTemplateName();
            try {
                //绝对路径
                String targetFile = templatePath + "/" + templateName;
                response.setCharacterEncoding("utf-8");
                response.setContentType("multipart/form-data");
                response.setHeader("Content-Disposition","attachment;fileName=" + FileUtils.setFileDownloadHeader(request, templateName));
                FileUtils.writeBytes(targetFile, response.getOutputStream());
            } catch (Exception e) {
                log.error("下载文件失败", e);
            }
    
        }
    

    FileUtils中,对下载的文件名重新编码(setFileDownloadHeader)

        /**
         * 下载文件名重新编码
         *
         * @param request  请求对象
         * @param fileName 文件名
         * @return 编码后的文件名
         */
        public static String setFileDownloadHeader(HttpServletRequest request, String fileName)
                throws UnsupportedEncodingException {
            final String agent = request.getHeader("USER-AGENT");
            String filename = fileName;
            if (agent.contains("MSIE")) {
                // IE浏览器
                filename = URLEncoder.encode(filename, "utf-8");
                filename = filename.replace("+", " ");
            } else if (agent.contains("Firefox")) {
                // 火狐浏览器
                filename = new String(fileName.getBytes(), "ISO8859-1");
            } else if (agent.contains("Chrome")) {
                // google浏览器
                filename = URLEncoder.encode(filename, "utf-8");
            } else {
                // 其它浏览器
                filename = URLEncoder.encode(filename, "utf-8");
            }
            return filename;
        } 

    FileUtis中读取文件流(writeBytes)

        /**
         * 输出指定文件的byte数组
         *
         * @param filePath 文件路径
         * @param os       输出流
         * @return
         */
        public static void writeBytes(String filePath, OutputStream os) throws IOException {
            FileInputStream fis = null;
            try {
                File file = new File(filePath);
                if (!file.exists()) {
                    throw new FileNotFoundException(filePath);
                }
                fis = new FileInputStream(file);
                byte[] b = new byte[1024];
                int length;
                while ((length = fis.read(b)) > 0) {
                    os.write(b, 0, length);
                }
            } catch (IOException e) {
                throw e;
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    

     至此ok。 

  • 相关阅读:
    android.permission
    Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
    人要怎样活在现实生活中
    viewpager---01
    Android ViewPager多页面滑动切换以及动画效果
    【205】C#实现远程桌面访问
    【204】显示3D大球球
    【203】利用UltraISO制作和刻录光盘映像的方法
    【202】ThinkPad手势&快捷键
    【201】SeaDAS代码
  • 原文地址:https://www.cnblogs.com/gfbzs/p/12864783.html
Copyright © 2011-2022 走看看