zoukankan      html  css  js  c++  java
  • SpringBoot访问jar包静态文件

    背景

    项目开发过程中我们我们会遇到访问静态文件的情况,例如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临时文件成功");
        }
    
  • 相关阅读:
    Java 使用Calendar类输出指定年份和月份的日历
    ioc aop
    多线程下单例模式:懒加载(延迟加载)和即时加载
    Java生产环境下性能监控与调优详解
    springboot + zipkin + mysql
    springboot + zipkin(brave-okhttp实现)
    springboot启动方式
    OpenResty实现限流的几种方式
    RocketMQ核心技术精讲与高并发抗压实战
    codis 使用
  • 原文地址:https://www.cnblogs.com/yanpeng19940119/p/15611867.html
Copyright © 2011-2022 走看看