0

I get the following error:

java.nio.file.NoSuchFileException: \webapp\src\main\webapp\resources\pics\default.jpg
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
//it goes on and on

Anyways the exception seems to be caused when I create the new File

File fi = new File("/webapp/src/main/webapp/resources/pics/default.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath());
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<byte[]>(fileContent, headers, HttpStatus.OK);

I assume it's related to the image path but I tried several things and nothing worked. I'm using spring-mvc and here's how my folders look. default.jpg is located inside of pics and the code above is a portion of HelloWorldController

I was able to get images in my jsp files, but I assume its different here. I've seen other answers but I don't really know how to apply those solutions. Other answers suggest the use of InputStream but I don't think I can use that in this case.

enter image description here

Flama
  • 772
  • 3
  • 13
  • 39
  • You need to package your application so that the image is on the classpath. Your source code is **not** and should not be on the classpath. Check to see if this file has been deployed to your webapp. – Scary Wombat Sep 28 '18 at 02:26
  • @ScaryWombat How can I package my application? Also other answes suggest the use of inputStream but I don't think I can use that if I want to create a new File. – Flama Sep 28 '18 at 13:03
  • i already solved it with the duplicate question. I also had to instance servletContext at the beginning of my controller. – Flama Sep 28 '18 at 17:05

0 Answers0