springboot 運行時復制jar中的文件夾到本地 FileUtils


場景: springboot項目運行時從配置文件夾 加載多個配置,讀取路徑:Paths.get("src/main/resources/conf"), 在idea中運行是可以讀取到的

但是以jar 的方式運行,加載jar中的文件夾,配置無法加載.

換成讀取jar中的絕對路徑 String path = ClassUtils.getDefaultClassLoader().getResource("conf").getPath();

依然不行。 

 

解決方案 項目啟動加載配置時,先復制 jar 中的文件夾到本地 /tmp/xxxconf  ,然后加載本地文件夾下的配置      fileUtils

FileUtils:

package com.icil.bx.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.*;

/**
 * @PACKAGE : com.icil.bx.common.utils
 * @Author :  Sea
 * @Date : 12/22/21 11:08 AM
 * @Desc :
 **/
@Slf4j
public class FileUtils
{
    /**
     * 復制單個文件 從classpath中讀取文件復制
     * @param path  不能以/開頭   指向文件不能是目錄
     * @param newpath   指向文件不能是目錄
     */
    public static void copyFileFromJar(String path,String newpath) {
        try {
            //創建新文件
            makeFile(newpath);
            //獲取文件流
            InputStream in =new String().getClass().getClassLoader().getResourceAsStream(path);
            //將流寫入新文件
            write2File(in, newpath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 復制path目錄下所有文件
     * @param path  文件目錄 不能以/開頭
     * @param newpath 新文件目錄
     */
    public static void dirCopyFileFromJar(String path,String newpath) {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //獲取所有匹配的文件
            Resource[] resources = resolver.getResources(path+"/*");
            //打印有多少文件
            log.info("*****************"+resources.length);
            for(int i=0;i<resources.length;i++) {
                Resource resource=resources[i];
                try {
                    //以jar運行時,resource.getFile().isFile() 無法獲取文件類型,會報異常,抓取異常后直接生成新的文件即可;以非jar運行時,需要判斷文件類型,避免如果是目錄會復制錯誤,將目錄寫成文件。
                    if(resource.getFile().isFile()) {
                        makeFile(newpath+"/"+resource.getFilename());
                        InputStream stream = resource.getInputStream();
                        write2File(stream, newpath+"/"+resource.getFilename());
                    }
                }catch (Exception e) {
                    log.info("-------------"+e.getMessage());
                    makeFile(newpath+"/"+resource.getFilename());
                    InputStream stream = resource.getInputStream();
                    write2File(stream, newpath+"/"+resource.getFilename());
                }
            }
        } catch (Exception e) {
            log.info("-------------"+e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 創建文件
     * @param path  全路徑 指向文件
     * @return
     */
    public static boolean makeFile(String path) {
        File file = new File(path);
        if(file.exists()) {
            log.info("文件已存在!");
            return false;
        }
        if (path.endsWith(File.separator)) {
            log.info("不能為目錄!");
            return false;
        }
        if(!file.getParentFile().exists()) {
            if(!file.getParentFile().mkdirs()) {
                log.info("創建目標文件所在目錄失敗!");
                return false;
            }
        }
        try {
            if (file.createNewFile()) {
                log.info("創建文件" + path + "成功!");
                return true;
            } else {
                log.info("創建文件" + path + "失敗!");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.info("創建文件" + path + "失敗!" + e.getMessage());
            return false;
        }
    }

    /**
     * 輸入流寫入文件
     * @param is 輸入流
     * @param filePath 文件保存目錄路徑
     * @throws IOException
     */
    public static void write2File(InputStream is, String filePath) throws IOException {
        OutputStream os = new FileOutputStream(filePath);
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((len = is.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
    }
}

 


免責聲明!

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



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