我在做新的Spring引导应用程序,并希望能够存储并提供图片,我想的图像将被存储在应用程序目录显示 here春季启动图像上传和服务
这是上传的样子现在:
@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
我应该怎么做才能使它工作?
谢谢:d –
欢迎您:) –