SpringBoot: java.io.FileNotFoundException: file [] cannot be resolved in the file system for checking its content length

Arjun :

I'm trying to return an image from a springboot controller as the Resource object but I'm getting the error java.io.FileNotFoundException: file [/home/user/eclipse-workspace/MedVibes-1/ironman.png] cannot be resolved in the file system for checking its content length

Controller

@RestController
    public class MedVibesController {
        @Autowired
        ResourceLoader resourceLoader;
        @RequestMapping(value = "/file", method = RequestMethod.GET)
        @ResponseBody
        Resource resource = new FileSystemResource("ironman.png");
    return resource;
    }

I've an ironman.png file in both the MedVibes-1 directory as well as it's static folder enter image description here

Still I'm getting the FileNOtFoundException? What's wrong with my code?How to fix this error?

pvpkiran :

FileSystemResource looks for the file in the FileSystem. if you want to use this, you have to specify full path.

You can use ClassPathResource

Resource resource = new ClassPathResource("static/ironman.png");

By default, it will look into resources folder.

Another way

@Autowired
ResourceLoader resourceLoader;

Resource resource = resourceLoader.getResource("classpath:ironman.png");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371655&siteId=1