我试图从文件系统中抓取一个文件(在这种情况下是一个图像)并显示它。我可以从资源子目录中执行它就好了,但是当我尝试转到文件系统时,它给了我一个 FileNotFound 异常。
java.io.FileNotFoundException: file:\Y:\Kevin\downloads\pic_mountain.jpg(文件名、目录名或卷标语法不正确)
我的所有其余代码都是从 Initialize 生成的 vanilla spring boot。谢谢。
@RestController
public class ImageProducerController {
@GetMapping("/get-text")
public @ResponseBody String getText() {
return "Hello World";
}
@GetMapping(value = "/get-jpg", produces = MediaType.IMAGE_JPEG_VALUE)
public void getImage(HttpServletResponse response) throws IOException {
FileSystemResource imgFile = new FileSystemResource("file:///Y:/Kevin/downloads/pic_mountain.jpg");
// ClassPathResource imgFile = new ClassPathResource("images/pic_mountain.jpg");
System.out.println(imgFile.getURL());
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
StreamUtils.copy(imgFile.getInputStream(), response.getOutputStream());
}
}
Run Code Online (Sandbox Code Playgroud)