英文:
Spring boot and thymeleaf - upload and display image
问题
我正在制作一个简单的Spring Boot应用程序,使用Thymeleaf。我有一个控制器用于上传图像,它保存在项目文件夹*uploads*中。请问有人可以解释一下,我如何在上传后立即显示它?我应该使用@PostMapping还是一些Thymeleaf标签?
**我的控制器类**
```java
@Controller
public class UploadFileController {
public static String uploadDirectory = System.getProperty("user.dir") + "/uploads";
@RequestMapping("/")
public String uploadPage(Model model) {
return "uploadview";
}
@RequestMapping("/upload")
public String upload(Model model, @RequestParam("files")MultipartFile[] files) {
StringBuilder fileNames = new StringBuilder();
for(MultipartFile file : files) {
Path fileNameAndPath = Paths.get(uploadDirectory, file.getOriginalFilename());
fileNames.append(file.getOriginalFilename());
try {
Files.write(fileNameAndPath, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
model.addAttribute("msg", "Uploaded files: " + fileNames.toString());
return "uploadstatusview";
}
更新
我正在添加如何应该显示的屏幕截图。
- 选择文件
- 选择的文件,例如test1.png - 上传文件
- 然后它会将我发送到另一个具有上传端点的HTML模板。到这一步它是可以工作的。图片保存在项目中,但现在我想看到这张图片,就像屏幕截图中展示的那样想要是这样的。
<details>
<summary>英文:</summary>
I'm making a simple spring boot app with Thymeleaf. I have a controller to upload images, it's saving in project folder *uploads*. Can please someone explain to me how can I display it instantly after uploading it? Should I use @PostMapping or just some Thymeleaf tag?
**My controller class**
@Controller
public class UploadFileController {
public static String uploadDirectory = System.getProperty("user.dir") + "/uploads";
@RequestMapping("/")
public String uploadPage(Model model) {
return "uploadview";
}
@RequestMapping("/upload")
public String upload(Model model, @RequestParam("files")MultipartFile[] files) {
StringBuilder fileNames = new StringBuilder();
for(MultipartFile file : files) {
Path fileNameAndPath = Paths.get(uploadDirectory, file.getOriginalFilename());
fileNames.append(file.getOriginalFilename());
try {
Files.write(fileNameAndPath, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
model.addAttribute("msg", "Uploaded files: " + fileNames.toString());
return "uploadstatusview";
}
**UPDATE**
I'm adding screenshots how it should look.
1. Choose file
2. Chosen file e.g test1.png - Upload file
3. Than its sending me on another html template with upload endpoint. To this point it's working. Picture it's saving in project but now I wanna see this picture like it's on screenshot [ want it to be like that][1]
[1]: https://i.stack.imgur.com/ylYhf.png
</details>
# 答案1
**得分**: 0
如果我理解正确,您想通过请求获取新上传的图像,位于单独的 URL。您可以在处理上传时将图像存储到列表或队列中,然后通过请求为存储在列表中的每个图像编译模板。
您的模板可能如下所示:
```html
<img src="${imgPath}" />
英文:
If I understood correctly, you want to get newly uploaded images by request at separate url. You can store images into List or Queue when handling upload, and then, by request, compile template for each image stored in List<br>
Your template might look like:
<img src="${imgPath}" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论