背景

我的项目开发过程中咱们咱们会遇到拜访动态文件的状况,例如word书签模板,excel导入模板,条文法规文件等,在war包的状况下拜访是没有问题的,如果应用jar包部署,应用相对路径拜访会呈现问题,本文就此问题给出解决方案。

配置

resources文件夹下创立动态目录systemfile,放入测试文件test.docx(文件名须要命名为英文)

pom文件resource/build节点设置打包编译疏忽systemfile文件夹

        <resources>            <resource>                <filtering>true</filtering>                <directory>src/main/resources</directory>                <excludes>                    <exclude>systemfile/*</exclude>                </excludes>            </resource>            <resource>                <filtering>false</filtering>                <directory>src/main/resources</directory>                <includes>                    <include>systemfile/*</include>                </includes>            </resource>        </resources>

拜访

应用ClassPathResource的getInputStream获取jar包中的文件的流暂存到磁盘的临时文件中,间接拜访临时文件即可

String testFilePath = ClassPathFileUtil.getFilePath("systemfile/test.docx");
public static String getFilePath(String classFilePath) {        String filePath = "";        try {            String templateFilePath = "tempfiles/classpathfile/";            File tempDir = new File(templateFilePath);            if (!tempDir.exists()) {                tempDir.mkdirs();            }            String[] filePathList = classFilePath.split("/");            String checkFilePath = "tempfiles/classpathfile";            for (String item : filePathList) {                checkFilePath += "/" + item;            }            File tempFile = new File(checkFilePath);            if (tempFile.exists()) {                filePath = checkFilePath;            } else {                //解析                ClassPathResource classPathResource = new ClassPathResource(classFilePath);                InputStream inputStream = classPathResource.getInputStream();                checkFilePath = "tempfiles/classpathfile";                for (int i = 0; i < filePathList.length; i++) {                    checkFilePath += "/" + filePathList[i];                    if (i==filePathList.length-1) {                        //文件                        File file = new File(checkFilePath);                        FileUtils.copyInputStreamToFile(inputStream, file);                    }else{                        //目录                        tempDir = new File(checkFilePath);                        if (!tempDir.exists()) {                            tempDir.mkdirs();                        }                    }                }                inputStream.close();                filePath = checkFilePath;            }        } catch (Exception e) {            e.printStackTrace();        }        return filePath;    }

留神

我的项目启动时,须要革除动态文件的临时文件,防止文件更新

@Component@Order(value = 10)public class StartUpContext  implements ApplicationRunner {    @Override    public void run(ApplicationArguments args) throws Exception {        String templateFilePath = "tempfiles/classpathfile/";        File tempDir = new File(templateFilePath);        FileSystemUtils.deleteRecursively(tempDir);        System.out.println("革除classpathfile临时文件胜利");    }