zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    Java学习总结
    John 尼姆博弈
    博弈知识汇总(转)
    坏习纠正
    QDUOJ 河老师的新年礼物(尺取法)
    HDU
    QDUOJ ycb的ACM进阶之路 二进制多重背包
    HDU
    HDU
    POJ
  • 原文地址:https://www.cnblogs.com/lshan/p/15718907.html
Copyright © 2011-2022 走看看