我正在尝试访问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,
文件文件 = 新文件(新类路径资源(“/src/main/resources/XYZ/view/abc.xsd”).getPath());模式模式 = 工厂
它正在工作,我想知道为什么其他两条路会出错,或者为什么它对我不起作用而对其他人却很好。:(
还是有其他好的方法,我错过了
你可以像这样使用贝娄..
我用过像Spring靴一样的风箱
@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();
}
@Value
和ResourceLoader对我来说都很好。我在
方法读取它。<="">
也许
静态
关键字是罪魁祸首?
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);
}
}
}
在我的Spring Boot加载资源教程中提供了完整的工作代码示例。
@Value
注释用于将属性值注入变量,通常是String或简单的原语值。你可以在这里找到更多信息。
如果要加载资源文件,请使用如下ResourceLoader
:
@Autowired
private ResourceLoader resourceLoader;
...
final Resource fileResource = resourceLoader.getResource("classpath:XYZ/view/abc.xsd");
然后,您可以使用以下命令访问资源:
fileResource.getInputStream()
或 fileResource.getFile()
我想问一下,如何在SpringBoot中将文件夹作为资源或文件加载。假设我有src/main/resources/testfolder,我想做如下事情: ^但这将失败,因为ResourceUtils只能加载实际文件(.txt、.csv等),不是文件夹。。提前非常感谢。。 编辑: 原因是我需要得到文件夹的绝对路径... folderpath应该是“/workspace/intellij/Projec
我正在从主类访问资源文件夹中的文件 我收到了这个错误: 我甚至打开了jar文件,remoteUnitsIdsInOldServer.txt文件就在那里,在类内部
有没有办法说服Gradle接受文件夹中的资源文件(如XML文件)? 这将是伟大的,因为我需要把我的JavaFXXML文件放在那里。。。
我有一个资源文件夹/包在我的项目的根,我"不"想要加载某个文件。如果我想加载某个文件,我会使用class.getResourceAsStream,我会没事的!!我实际上想做的是在资源文件夹中加载一个“文件夹”,循环该文件夹中的文件,并获得每个文件的流,并在内容中读取...假设在运行时之前没有确定文件名...那我该怎么办?有没有一种方法来获得一个文件夹中的文件列表在您的jar文件?请注意,带有资源的
我有一个spring boot应用程序,我想在其中外部化消息。我将这些消息分为错误、信息和成功。因此,我创建了如下所示的嵌套文件夹结构: 并且,我正试图通过以下方式从服务中访问它: 这给了我以下例外情况: missingResourceException:找不到bundle的基本名称错误,区域设置为 但是,如果我将属性文件保存在下面的文件夹之外,它可以正常工作: null
Spring靴-1.4.1;IDE-IntelliJ 2016.3。EAP 我试图加载资源如下; 和 当我从IDE运行应用程序时,这两种方法都可以正常工作,但当我尝试将应用程序构建为war并将其部署到Tomcat上时,这两种方法都不起作用。 有人能帮我解决这个问题吗? 谢谢