小编Wer*_*dve的帖子

Spring Boot图像上传和服务

我在做新的Spring引导应用程序,并希望能够存储并提供图片,我想的图像将被存储在应用程序目录显示 在这里

这就是现在上传的样子:

@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";
}
Run Code Online (Sandbox Code Playgroud)

这就是我想要获取图像的方式

<img th:src="@{'images/' + ${post.imageName}}"/>
Run Code Online (Sandbox Code Playgroud)

现在我得到404,当我想查看我得到的目录中的一些图像

Fatal error reading PNG image file: Not a PNG file
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能让它发挥作用?

spring file-upload image static-content spring-boot

7
推荐指数
2
解决办法
2万
查看次数

找不到Spring Boot JSP

在我的Spring Boot应用程序中,我从Thymeleaf切换到JSP,并且在我的本地计算机上正常运行,但是当我将其上传到Heroku服务器时,我得到了
There was an unexpected error (type=Not Found, status=404). /WEB-INF/jsp/home.jsp

它的jar打包了,如果它在本地运行而不在Heroku服务器上工作,这意味着什么?

编辑,项目结构 在此处输入图片说明

spring jsp heroku spring-boot

1
推荐指数
1
解决办法
4669
查看次数

标签 统计

spring ×2

spring-boot ×2

file-upload ×1

heroku ×1

image ×1

jsp ×1

static-content ×1