【问题标题】:How to access a resource file in src/main/resources/ folder in Spring Boot如何在 Spring Boot 中访问 src/main/resources/ 文件夹中的资源文件
【发布时间】:2017-10-03 21:29:04
【问题描述】:

我正在尝试访问 src/main/resources/XYZ/view 文件夹中的 xsd,其中 XYZ/view 文件夹是我创建的,并且文件夹具有我需要进行 xml 验证的 abc.xsd。

当我每次都尝试访问 xsd 时,我得到的结果为 null,

我试过了,

1)

@Value(value = "classpath:XYZ/view/abc.xsd")
private static Resource dataStructureXSD;
InputStream is = dataStructureXSD.getInputStream();
Source schemaSource = new StreamSource(is);
Schema schema = factory.newSchema(schemaSource);

2)

Resource resource = new ClassPathResource("abc.xsd");
File file = resource.getFile();

以及我为获取资源或类加载器等所做的更多路径。

最后我得到了xsd,

文件 file = new File(new ClassPathResource("/src/main/resources/XYZ/view/abc.xsd").getPath()); Schema schema = factory.newSchema(file);

它正在工作,我想知道为什么其他两条路径会出错,或者为什么它对我不起作用而对其他人很好。 :(

或者还有其他我想念的好方法

【问题讨论】:

    标签: java spring-boot jaxb


    【解决方案1】:

    @Value annotation 用于将属性值注入变量,通常是字符串或简单的原始值。您可以找到更多信息here

    如果要加载资源文件,请使用 ResourceLoader 之类的:

    @Autowired
    private ResourceLoader resourceLoader;
    
    ...
    
    final Resource fileResource = resourceLoader.getResource("classpath:XYZ/view/abc.xsd");
    

    然后您可以通过以下方式访问资源:

    fileResource.getInputStream()fileResource.getFile()

    【讨论】:

    • fileResource.getFile() 是否适用于 Spring Boot jar 中的文件?
    【解决方案2】:

    @ValueResourceLoader 都适合我。我在src/main/resources/ 中有一个简单的文本文件,我可以用这两种方法阅读它。

    也许static 关键字是罪魁祸首?

    package com.zetcode;
    
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.ResourceLoader;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyRunner implements CommandLineRunner {
    
        @Value("classpath:thermopylae.txt")
        private Resource res;
    
        //@Autowired
        //private ResourceLoader resourceLoader;
    
        @Override
        public void run(String... args) throws Exception {
    
           // Resource fileResource = resourceLoader.getResource("classpath:thermopylae.txt");        
    
            List<String> lines = Files.readAllLines(Paths.get(res.getURI()),
                    StandardCharsets.UTF_8);
    
            for (String line : lines) {
    
                System.out.println(line);
    
            }
        }
    }
    

    我的Loading resouces in Spring Boot 教程中提供了完整的工作代码示例。

    【讨论】:

      【解决方案3】:

      你可以像 ..一样使用 bellow

      1. 我的资源文件位置如下所示

      我在 spring boot 中使用了波纹管

      @Autowired
      private ResourceLoader resourceLoader;
      
        try {     
      
         final Resource resource = resourceLoader.getResource("classpath:files/timezoneJson.json");
               Reader reader = new InputStreamReader(resource.getInputStream());
               String filedata =  FileCopyUtils.copyToString(reader);
          } catch (Exception e) {     
              e.printStackTrace();
          }
      

      【讨论】:

        【解决方案4】:

        从资源加载文件,

        @自动连线 私有资源加载器资源加载器; ...

        资源文件Resource = resourceLoader.getResource(config.getFilePath()); 尝试 (BufferedReader in = new BufferedReader(new FileReader(fileResource.getFile()))){ ... }

        在 applicaiton.yml 中

        filePath: "classpath:StaticData/Test.csv"
        

        【讨论】:

          猜你喜欢
          • 2017-02-05
          • 2014-11-21
          • 2018-05-28
          • 2020-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-22
          相关资源
          最近更新 更多