spring boot jar运行时复制jar中文件

当spring boot项目以jar运行时 复制文件就不能根据路径来复制了,这时需要先获取文件流然后生成新的文件,下面介绍两种方法,原理都是一样的,取流复制:
1.复制单个文件

/**
     * 复制单个文件 从classpath中读取文件复制
     * @param path  不能以/开头   指向文件不能是目录
     * @param newpath   指向文件不能是目录
     */
    public static void copyFileFromJar(String path,String newpath) {
        try {
            //创建新文件
            makeFile(newpath);
            //获取文件流
            InputStream in = FileUtil.class.getClassLoader().getResourceAsStream(path);
            //将流写入新文件
            write2File(in, newpath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

例子:从templates文件夹下复制index.html到/home目录下
copyFileFromJar(“templates/index.html”,”/home/index.html”)

2.复制指定目录下所有文件,不能递归

/**
     * 复制path目录下所有文件  
     * @param path  文件目录 不能以/开头
     * @param newpath 新文件目录
     */
    public static void BatCopyFileFromJar(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();
        }
    }

例子:从templates下复制publish目录下的文件到/home下
BatCopyFileFromJar(“templates/publish”,”/home”)

以上就是两种从运行的jar中复制文件的方法。
补充代码:

/**
     * 创建文件
     * @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();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值