春季引导和Thymeleaf – 上传并显示图片

huangapple go评论59阅读模式
英文:

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";
    }

更新
我正在添加如何应该显示的屏幕截图。

  1. 选择文件
  2. 选择的文件,例如test1.png - 上传文件
  3. 然后它会将我发送到另一个具有上传端点的HTML模板。到这一步它是可以工作的。图片保存在项目中,但现在我想看到这张图片,就像屏幕截图中展示的那样想要是这样的

<details>
<summary>英文:</summary>

I&#39;m making a simple spring boot app with Thymeleaf. I have a controller to upload images, it&#39;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(&quot;/&quot;)
public String uploadPage(Model model) {
    return &quot;uploadview&quot;;
}

@RequestMapping(&quot;/upload&quot;)
public String upload(Model model, @RequestParam(&quot;files&quot;)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(&quot;msg&quot;, &quot;Uploaded files: &quot; + fileNames.toString());
    return &quot;uploadstatusview&quot;;
}

**UPDATE**
I&#39;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&#39;s working. Picture it&#39;s saving in project but now I wanna see this picture like it&#39;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:

&lt;img src=&quot;${imgPath}&quot; /&gt;

huangapple
  • 本文由 发表于 2020年8月28日 20:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定