【发布时间】:2018-01-20 21:51:55
【问题描述】:
我正在制作新的 Spring Boot 应用程序并希望能够存储和提供图像,我希望将图像存储在应用程序目录中:
这就是现在上传的样子:
@PostMapping("/")
@ResponseBody
public String upload(@RequestPart String title, @RequestPart MultipartFile img) throws IOException{
String imgName = img.getOriginalFilename();
Post p = new Post();
p.setTitle(title);
p.setImageName(imgName);
postService.add(p);
File upl = new File("images/" + imgName);
upl.createNewFile();
FileOutputStream fout = new FileOutputStream(upl);
fout.write(img.getBytes());
fout.close();
return "ok";
}
这就是我想要获取图像的方式
<img th:src="@{'images/' + ${post.imageName}}"/>
现在我得到 404,当我想查看目录中的一些图像时,我得到了
Fatal error reading PNG image file: Not a PNG file
我应该怎么做才能让它发挥作用?
【问题讨论】:
标签: spring image spring-boot file-upload static-content