小编Anv*_*ddy的帖子

使用docker容器中的java访问类路径中的JSON文件时遇到FileNotFoundException(SprintBootApplication)

我在使用docker容器加载Java jar类路径中的JSON文件时遇到FileNotFoundException,它是一个Spring-Boot应用程序.此JSON文件在资源文件夹中可用.我能够在./target/classes/path下的docker中看到JSON文件.

Resource resource = resourceLoader.getResource("classpath:folderNm/file.json");
HashMap<String, String> headerMapping = (HashMap<String, String>) parser.parse(new FileReader(resource.getFile().getAbsolutePath()));
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个例外:

java.io.FileNotFoundException: class path resource [folderNm/file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/folderNm/file.json
Run Code Online (Sandbox Code Playgroud)

我试过了

- > resource.getFile().getPath(); - > resource.getFile().getCanonicalPath(); - >" ./target/classes/folderName/fileName"(硬编码的FilePath位置) - >" /app.jar!/folderNm/file.json"(硬编码的FilePath位置)

InputStream inputStream = getClass().getResourceAsStream("xyz.json");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = br.readLine()) != null)
                responseStrBuilder.append(inputStr);
Run Code Online (Sandbox Code Playgroud)

以上都没有运行.请建议一种解决此问题的方法.

java linux json docker spring-boot

7
推荐指数
1
解决办法
2772
查看次数

标签 统计

docker ×1

java ×1

json ×1

linux ×1

spring-boot ×1