iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot实现jar运行复制resources文件到指定的目录(思路详解)
  • 571
分享到

springboot实现jar运行复制resources文件到指定的目录(思路详解)

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot实现jar运行复制resources文件到指定的目录1. 需求2. 思路3. 实现代码4. 工具类FreeMarkerUtil5.效果springboot实现

springboot实现jar运行复制resources文件到指定的目录

1. 需求

项目开发过程中需要将项目resources/static/目录下所有资源资源复制到指定目录。公司项目中需要下载视频文件,由于下载的有个html页面,对多路视频进行画面加载,用到对应的静态资源文件,如js,CSS.jwplayer,Jquery.js等文件

Maven打成的jar和平时发布的项目路径不通,所以在读取路径的时候获取的是jar的路径,无法获取jar里面的文件路径

2. 思路

根据我的需求,复制的思路大概是,先获取到resources/static/blog目录下文件清单,然后通过清单,循环将文件复制到指定位置(相对路径需要确保一致)

因为项目会被打成jar包,所以不能用传统的目录文件复制方式,这里需要用到Spring Resource:

Resource接口,简单说是整个Spring框架对资源的抽象访问接口。它继承于InputStreamSource接口。后续文章会详细的分析。

Resource接口的方法说明:

方法说明
exists()判断资源是否存在,true表示存在。
isReadable()判断资源的内容是否可读。需要注意的是当其结果为true的时候,其内容未必真的可读,但如果返回false,则其内容必定不可读。
isOpen()判断当前Resource代表的底层资源是否已经打开,如果返回true,则只能被读取一次然后关闭以避免资源泄露;该方法主要针对于InputStreamResource,实现类中只有它的返回结果为true,其他都为false。
getURL()返回当前资源对应的URL。如果当前资源不能解析为一个URL则会抛出异常。如ByteArrayResource就不能解析为一个URL。
getURI()返回当前资源对应的URI。如果当前资源不能解析为一个URI则会抛出异常。
getFile()返回当前资源对应的File。
contentLength()返回当前资源内容的长度
lastModified()返回当前Resource代表的底层资源的最后修改时间。
createRelative()根据资源的相对路径创建新资源。[默认不支持创建相对路径资源]
getFilename()获取资源的文件名。
getDescription()返回当前资源底层资源的描述符,通常就是资源的全路径(实际文件名或实际URL地址)。
getInputStream()获取当前资源代表的输入流。除了InputStreamResource实现类以外,其它Resource实现类每次调用getInputStream()方法都将返回一个全新的InputStream。

获取Resource清单,我需要通过ResourceLoader接口获取资源,在这里我选择了

org.springframework.core.io.support.PathMatchingResourcePatternResolver

3. 实现代码


    private void copyJwplayer() {
        //判断指定目录下文件是否存在
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        String rootpath = applicationHome.getSource().getParentFile().toString();
        String realpath=rootpath+"/vod/jwplayer/";   //目标文件
        String silderrealpath=rootpath+"/vod/jwplayer/silder/";   //目标文件
        String historyrealpath=rootpath+"/vod/jwplayer/history/";   //目标文件
        String jwplayerrealpath=rootpath+"/vod/jwplayer/jwplayer/";   //目标文件
        String layoutrealpath=rootpath+"/vod/jwplayer/res/layout/";   //目标文件
        
        //不使用getFileFromClassPath()则报[static/jwplayerF:/workspace/VRSH265/target/classes/static/jwplayer/flvjs/] cannot be resolved to URL because it does not exist
        //jwplayer
        File fileFromClassPath = FreeMarkerUtil.getFileFromClassPath("/static/jwplayer/");  //复制目录
        FreeMarkerUtil.BatCopyFileFromJar(fileFromClassPath.toString(),realpath);
        //silder
        File flvjspath = FreeMarkerUtil.getFileFromClassPath("/static/jwplayer/silder/");  //复制目录
        FreeMarkerUtil.BatCopyFileFromJar(flvjspath.toString(),silderrealpath);
        //history
        File historypath = FreeMarkerUtil.getFileFromClassPath("/static/jwplayer/history/");  //复制目录
        FreeMarkerUtil.BatCopyFileFromJar(historypath.toString(),historyrealpath);
        //jwplayer
        File jwplayerpath = FreeMarkerUtil.getFileFromClassPath("/static/jwplayer/jwplayer/");  //复制目录
        FreeMarkerUtil.BatCopyFileFromJar(jwplayerpath.toString(),jwplayerrealpath);
        //layout
        File layoutpath = FreeMarkerUtil.getFileFromClassPath("/static/jwplayer/res/layout/");  //复制目录
        FreeMarkerUtil.BatCopyFileFromJar(layoutpath.toString(),layoutrealpath);
    }

4. 工具类FreeMarkerUtil

package com.aio.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.*;



public class FreeMarkerUtil {

   
    
    public static void BatCopyFileFromJar(String path,String newpath) {
        if (!new File(newpath).exists()){
            new File(newpath).mkdir();
        }
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //获取所有匹配的文件
            Resource[] resources = resolver.getResources(path+"
    public static boolean makeFile(String path) {
        File file = new File(path);
        if(file.exists()) {
            return false;
        }
        if (path.endsWith(File.separator)) {
            return false;
        }
        if(!file.getParentFile().exists()) {
            if(!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        try {
            if (file.createNewFile()) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    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();
    }
    
    public static File getFileFromClassPath(String path){
        File targetFile = new File(path);
        if(!targetFile.exists()){
            if(targetFile.getParent()!=null){
                File parent=new File(targetFile.getParent());
                if(!parent.exists()){
                    parent.mkdirs();
                }
            }
            InputStream initialStream=null;
            OutputStream outStream =null;
            try {
                Resource resource=new ClassPathResource(path);
                //注意通过getInputStream,不能用getFile
                initialStream=resource.getInputStream();
                byte[] buffer = new byte[initialStream.available()];
                initialStream.read(buffer);
                outStream = new FileOutputStream(targetFile);
                outStream.write(buffer);
            } catch (IOException e) {
            } finally {
                if (initialStream != null) {
                    try {
                        initialStream.close(); // 关闭流
                    } catch (IOException e) {
                    }
                }
                if (outStream != null) {
                    try {
                        outStream.close(); // 关闭流
                    } catch (IOException e) {
                    }
                }
            }
        }
        return targetFile;
    }
}

5.效果

在这里插入图片描述

到此这篇关于springboot实现jar运行复制resources文件到指定的目录的文章就介绍到这了,更多相关springboot运行复制resources文件到指定的目录内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot实现jar运行复制resources文件到指定的目录(思路详解)

本文链接: https://www.lsjlt.com/news/209328.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作