FileUtils 文件工具類


FileUtils

下載jar中的文件

package com.meeno.chemical.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * 文件工具類
 */
@Slf4j
public class FileUtils {

    /**
     * 下載指定目錄文件
     * @param res
     * @param downFileName 下載文件名  xxx模板.xlsx
     * @param resourcePath 下載路徑   static/down/materielTemplate.xlsx
     */
    public static void downloadFile(HttpServletResponse res,String downFileName,String resourcePath){
        String fileName = downFileName;

        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream;charset=utf-8");
        try {
            res.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] buff = new byte[1024];
//        BufferedInputStream bis = null;
        InputStream bis = null;
        OutputStream os = null;
       try {

           //第一種
           //ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx");
           //InputStream inputStream =classPathResource.getInputStream();

           //第二種
           //InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");

           //第三種
           //InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx");

           //第四種
           //File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx");
           //InputStream inputStream = new FileInputStream(file);

           //前三種方法在開發環境(IDE中)和生產環境(linux部署成jar包)都可以讀取到,第四種只有開發環境 時可以讀取到,生產環境讀取失敗。
           //推測主要原因是springboot內置tomcat,打包后是一個jar包,無法直接讀取jar包中的文件,讀取只能通過類加載器讀取。
           //前三種都可以讀取到其實殊途同歸,直接查看底層代碼都是通過類加載器讀取文件流,類加載器可以讀取jar包中的編譯后的class文件,當然也是可以讀取jar包中的excle模板了。

            //String path = ResourceUtils.getURL("classpath:static/projectRes/樊登.xlsx").getPath();
            //Resource resource = new ClassPathResource("static/projectRes/樊登子級用戶模板.xlsx");
           bis = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
           //Resource resource = new ClassPathResource(resourcePath);
            //File file = resource.getFile();
            os = res.getOutputStream();
            //bis = new BufferedInputStream(new FileInputStream(file));
            int i = bis.read(buff);

            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM