【发布时间】:2017-08-28 06:18:17
【问题描述】:
我正在尝试从文件系统中获取一个文件(在本例中为图像)并显示它。我可以从资源子目录中执行此操作,但是当我尝试转到文件系统时,它给了我一个 FileNotFound 异常。
java.io.FileNotFoundException: file:\Y:\Kevin\downloads\pic_mountain.jpg(文件名、目录名或卷标语法不正确)
我所有其余的代码都是从 Initialize.谢谢。
@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());
}
}
【问题讨论】:
-
为什么要这样访问文件系统??
-
您确实意识到这段代码永远不会在您的 PC 之外运行,对吧?
标签: java spring spring-boot