Java 如何在 Spring Boot 中访问 src/main/resources/ 文件夹中的资源文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43804262/
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
How to access a resource file in src/main/resources/ folder in Spring Boot
提问by tyro
I'm trying to access xsd in src/main/resources/XYZ/view folder where XYZ/view folder are created by me and folder has abc.xsd which I need for xml validation.
我正在尝试访问 src/main/resources/XYZ/view 文件夹中的 xsd,其中 XYZ/view 文件夹是由我创建的,文件夹中有 abc.xsd,我需要它进行 xml 验证。
When I try to access the xsd every time I get the result as null,
当我每次得到空结果时都尝试访问 xsd 时,
I have tried as,
我试过,
1)
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)
2)
Resource resource = new ClassPathResource("abc.xsd");
File file = resource.getFile();
and many more trails I made to get the resource or classloader etc.
以及我为获取资源或类加载器等而制作的更多路径。
Finally I get the xsd with,
最后我得到了 xsd,
File file = new File(new ClassPathResource("/src/main/resources/XYZ/view/abc.xsd").getPath()); Schema schema = factory.newSchema(file);
File file = new File(new ClassPathResource("/src/main/resources/XYZ/view/abc.xsd").getPath()); 架构架构 = factory.newSchema(file);
and it is working, I want to know why the other two trails would have gone wrong or why it didn't work for me and fine for others. :(
它正在起作用,我想知道为什么其他两条路径会出错,或者为什么它对我不起作用而对其他人很好。:(
Or is there other good way of doing it which I'm missing
或者还有其他我想念的好方法吗
采纳答案by Kevin Peters
The @Value
annotationis used to inject property values into variables, usually Strings or simple primitive values. You can find more info here.
的@Value
注释用于注入属性值到变量,通常是字符串或简单的原始值。您可以在此处找到更多信息。
If you want to load a resource file, use a ResourceLoader
like:
如果要加载资源文件,请使用ResourceLoader
类似:
@Autowired
private ResourceLoader resourceLoader;
...
final Resource fileResource = resourceLoader.getResource("classpath:XYZ/view/abc.xsd");
Then you can access the resource with:
然后你可以访问资源:
fileResource.getInputStream()
or fileResource.getFile()
fileResource.getInputStream()
或者 fileResource.getFile()
回答by Jan Bodnar
Both @Value
and ResourceLoader
work OK for me. I have a simple text file in src/main/resources/
and I was able to read it with both approaches.
这两个@Value
和ResourceLoader
我的工作确定。我有一个简单的文本文件src/main/resources/
,我可以用两种方法阅读它。
Maybe the static
keyword is the culprit?
也许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);
}
}
}
A full working code example is available in my Loading resouces in Spring Boottutorial.
我的Loading resouces in Spring Boot教程中提供了一个完整的工作代码示例。
回答by Enamul Haque
You may use bellow like ..
你可以像下面这样使用..
- My resource file location is like bellow
- 我的资源文件位置如下
I have use bellow like in 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();
}