0

Can someone explain me how do i read my jrxml as inputstream under my static resources.

This is my resigtered resource path

registry.addResourceHandler("/resources/**").addResourceLocations("WEB-INF/resources/");

under resources i have reports folder, under reports folder i have samplereport.jrxml

i use these codes but it always throws filenotfound exception

Resource resource = new ClassPathResource("/reports/samplereport.jrxml");
    InputStream resourceInputStream = resource.getInputStream();

I tried this and moved my samplereport.jrxml at the same path of my class and it work.

InputStream employeeReportStream
      = this.getClass().getResourceAsStream("/samplereport.jrxml");

But i dont want to work on this way. Thank you in advance.

RE0824C
  • 73
  • 1
  • 3
  • 12

3 Answers3

1

Remove slash before reports like below code.

Resource resource = new ClassPathResource("reports/samplereport.jrxml");

Refer below URL for more info regarding loading files.

https://www.mkyong.com/java/java-read-a-file-from-resources-folder/

How do I load a file from resource folder?

Alien
  • 15,141
  • 6
  • 37
  • 57
  • This is not working, java.io.FileNotFoundException: class path resource [reports/samplereport.jrxml] cannot be opened because it does not exist – RE0824C Jul 13 '18 at 01:22
0

you can use ResourceUtils class for reading resource locations to files in the file system.

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);

But, if you are using Spring then only ResourceUtils class is helps.(java.lang.Object.org.springframework.util.ResourceUtils)

0

You may made mistake in this path:

registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

instead of

registry.addResourceHandler("/resources/**").addResourceLocations("WEB-INF/resources/");
Rashed
  • 124
  • 12