zoukankan      html  css  js  c++  java
  • jar包内的文件导出的注意点

    1.截取文件名 windows 和linux 通用

            String fp[] = filePath.replaceAll("\\","/").split("/");
            String fileName = filePath;
            if (fp.length > 1) {
                fileName = fp[fp.length - 1];
            }

    2.文件名称转码

            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(),"iso-8859-1"));

    3.直接按路径下载会报这样的错误

    product-controller-0.0.1-SNAPSHOT.jar!BOOT-INFclasses!	emplate	ext.xls (No such file or directory)
    

    解决方案

    不读取工程中的文件地址,直接将对应文件转化为二进制流进行操作。

    InputStream is = null;
            is = AppUtil.class.getClassLoader().getResourceAsStream(filePath);

    4.附完整文件下载代码

    public static void download(HttpServletResponse response, String filePath) throws IOException {
    
    
            String fp[] = filePath.replaceAll("\\","/").split("/");
            String fileName = filePath;
            if (fp.length > 1) {
                fileName = fp[fp.length - 1];
            }
            //下载机器码文件
            response.setHeader("conent-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(),"iso-8859-1"));
    
            OutputStream os = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
            InputStream is = null;
            is = AppUtil.class.getClassLoader().getResourceAsStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(is);
    
            int length = 0;
            byte[] temp = new byte[1 * 1024 * 10];
    
            while ((length = bis.read(temp)) != -1) {
                bos.write(temp, 0, length);
            }
            bos.flush();
            bis.close();
            bos.close();
            is.close();
        }
  • 相关阅读:
    01_15_Struts2_带参数的结果集
    Splunk_导航菜单配置
    质量管理体系框架
    测试人员和测试Leader考核维度
    作为管理者口袋里面需要有多把武器,拿来即用
    产品质量体系——如何度量产品质量?
    adb 获取平台号
    模拟定位工具gps mock
    Android 电脑投屏工具Vysor Pro介绍
    Eclipse+Maven+TestNg+ReportNg 生成测试报告
  • 原文地址:https://www.cnblogs.com/zhucww/p/9208936.html
Copyright © 2011-2022 走看看