Java 在我的spring静态资源上读取文件总是空的
有人能告诉我如何在静态资源下以inputstream的形式读取jrxml吗 这是我调整大小的资源路径Java 在我的spring静态资源上读取文件总是空的,java,spring,spring-mvc,inputstream,Java,Spring,Spring Mvc,Inputstream,有人能告诉我如何在静态资源下以inputstream的形式读取jrxml吗 这是我调整大小的资源路径 registry.addResourceHandler("/resources/**").addResourceLocations("WEB-INF/resources/"); 在“资源”“我有报告”文件夹下,在“报告”文件夹下,我有samplereport.jrxml 我使用这些代码,但它总是抛出filenotfound异常 Resource resource = new ClassPath
registry.addResourceHandler("/resources/**").addResourceLocations("WEB-INF/resources/");
在“资源”“我有报告”文件夹下,在“报告”文件夹下,我有samplereport.jrxml
我使用这些代码,但它总是抛出filenotfound异常
Resource resource = new ClassPathResource("/reports/samplereport.jrxml");
InputStream resourceInputStream = resource.getInputStream();
我尝试了这一点,并将samplereport.jrxml移到了类的同一路径上,它正常工作
InputStream employeeReportStream
= this.getClass().getResourceAsStream("/samplereport.jrxml");
但是我不想这样做。提前感谢。删除斜杠后再报告,如下面的代码
Resource resource = new ClassPathResource("reports/samplereport.jrxml");
有关加载文件的更多信息,请参阅下面的URL
您可以使用
ResourceUtils
类将资源位置读取到文件系统中的文件中
File file = ResourceUtils.getFile("classpath:reports/file1.txt")
//File is found
System.out.println("File Found : " + file.exists());
//Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println(content);
但是,如果您使用的是Spring,那么只有
ResourceUtils
类才有帮助。(java.lang.Object.org.springframework.util.ResourceUtils
)您可能会在以下路径中出错:
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
而不是
registry.addResourceHandler("/resources/**").addResourceLocations("WEB-INF/resources/");
这不起作用,java.io.FileNotFoundException:无法打开类路径资源[reports/samplereport.jrxml],因为它不存在