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();
        }
  • 相关阅读:
    C# 函数参数object sender, EventArgs e
    Winform中利用委托实现窗体之间的传值
    Web前端学习笔记——Canvas
    js 删除 按钮所在的行
    box-sizing
    前端中关于HTML标签的属性for的理解
    apply和call的用法总结
    target 确定元素是谁??
    css3过渡和动画
    处理两端极限值的小技巧
  • 原文地址:https://www.cnblogs.com/zhucww/p/9208936.html
Copyright © 2011-2022 走看看