鍍金池/ 問答/Java/ springboot多模塊打包,模塊無法加載打包在本模塊的數(shù)據(jù)文件

springboot多模塊打包,模塊無法加載打包在本模塊的數(shù)據(jù)文件

springboot多模塊項目,其中web項目打包成jar運行,web項目依賴工具模塊xx.jar
xx.jar通過ClassLoader.getSystemResourceAsStream("xx.txt")讀取數(shù)據(jù)文件,單獨運行工具模塊可以正常運行
但是web項目依賴,打包進springboot可執(zhí)行jar后無法讀取到xx.jar需要的數(shù)據(jù)文件
請問各位我應(yīng)該如何讀取這個配置文件?
即如何訪問springboot的jar文件中/BOOT-INF/lib中jar文件中的文件

回答
編輯回答
萌二代

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);
            }
        }
2018年3月5日 23:57