关于springboot:SpringBoot访问jar包静态文件

10次阅读

共计 1990 个字符,预计需要花费 5 分钟才能阅读完成。

背景

我的项目开发过程中咱们咱们会遇到拜访动态文件的状况,例如 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 临时文件胜利");
    }
正文完
 0