SpringBoot访问jar包静态文件

x33g5p2x  于2022-02-20 转载在 Spring  
字(2.2k)|赞(0)|评价(0)|浏览(144)

背景

项目开发过程中我们我们会遇到访问静态文件的情况,例如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临时文件成功");
    }

相关文章

微信公众号

最新文章

更多