springboot多模块打包,模块无法加载打包在本模块的数据文件

springboot多模块项目,其中web项目打包成jar运行,web项目依赖工具模块xx.jar
xx.jar通过ClassLoader.getSystemResourceAsStream("xx.txt")读取数据文件,单独运行工具模块可以正常运行
但是web项目依赖,打包进springboot可执行jar后无法读取到xx.jar需要的数据文件
请问各位我应该如何读取这个配置文件?
即如何访问springboot的jar文件中/BOOT-INF/lib中jar文件中的文件

阅读 2.8k
1 个回答
新手上路,请多包涵

InputStream stream = ClassLoader.getSystemResourceAsStream("xx.txt");

        if (stream == null) {
            URL url = ClassLoader.getSystemResource("BOOT-INF");
            if (url != null) {
                String path = url.getFile();
                path = path.substring(5, path.lastIndexOf("!"));
                JarFile jarFile = new JarFile(path);
                JarEntry entry = jarFile.getJarEntry("BOOT-INF/lib/xx.jar");
                InputStream is = jarFile.getInputStream(entry);
                String folder = System.getProperty("java.io.tmpdir");
                File file = new File(folder, "xx.jar");
                OutputStream output = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
                    output.write(buffer, 0, bytesRead);
                }
                output.close();
                is.close();
                JarFile jar = new JarFile(file);
                JarEntry jarEntry = jar.getJarEntry("xx.txt");
                stream = jar.getInputStream(jarEntry);
            }
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题