java 从 BOOT-INF/classes 读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42655397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:47:56  来源:igfitidea点击:

Read files from BOOT-INF/classes

javaspringspring-boot

提问by CPA

I have a Spring Boot application with a java resource folder:

我有一个带有 java 资源文件夹的 Spring Boot 应用程序:

src
 |
  main
   |
    resources
     |
      test
       |
        test1.json
        test2.json
        ...

In the resource folder there are json files. I can read these files within my IDE (IntelliJ). But as a compiled JAR file, I get Nullpointerexceptions.

在资源文件夹中有 json 文件。我可以在我的 IDE (IntelliJ) 中读取这些文件。但是作为编译的 JAR 文件,我得到了Nullpointer例外。

Spring Boot copies the files to: BOOT-INF/classes/testIs it possible to read the resource files within a JAR file? I don't know the file names. So in first, I have to get all file names and the read each file.

Spring Boot 将文件复制到:BOOT-INF/classes/test是否可以读取 JAR 文件中的资源文件?我不知道文件名。所以首先,我必须获取所有文件名并读取每个文件。

Does anyone have an idea?

有没有人有想法?

UPDATE

更新

I have tried this:

我试过这个:

Resources[] resources = applicationContext.getResources("classpath*:**/test/*.json");

With that I'm getting all file paths. But that needs too much time. And even if I get the file names, how would I read the files?

有了这个,我得到了所有的文件路径。但这需要太多时间。即使我得到了文件名,我将如何读取文件?

采纳答案by artemisian

The below solution will read the files into a Map.

以下解决方案会将文件读入 Map。

Here you read your resources:

在这里您可以阅读您的资源:

Resource[] resources = applicationContext.getResources("classpath*:test/*.json"); 
for (Resource r: resources) {
     processResource(r);
}

Here you process your resources:

在这里处理您的资源:

// you need to add a dependency (if you don't have it already) for com.fasterxml.Hymanson.core:Hymanson-databind
ObjectMapper mapper = new ObjectMapper();

private void processResource(Resource resource) {
    try {
          Map<String, Object> jsonMap = mapper.readValue(resource.getInputStream(), Map.class);

          // do stuffs with your jsoMap

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

回答by abcdefgh

This actually works using a ResourcePatternResolver

这实际上可以使用 ResourcePatternResolver

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:test/*.json");

    for(Resource r: resources) {
        InputStream inputStream = r.getInputStream();
        File somethingFile = File.createTempFile(r.getFilename(), ".cxl");
        try {
            FileUtils.copyInputStreamToFile(inputStream, somethingFile);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        LicenseManager.setLicenseFile(somethingFile.getAbsolutePath());
        log.info("File Path is " + somethingFile.getAbsolutePath());
    }