Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我的spring静态资源上读取文件总是空的_Java_Spring_Spring Mvc_Inputstream - Fatal编程技术网

Java 在我的spring静态资源上读取文件总是空的

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

有人能告诉我如何在静态资源下以inputstream的形式读取jrxml吗

这是我调整大小的资源路径

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],因为它不存在