Spring Boot和thymeleaf-上传并显示图像

问题描述

我正在用Thymeleaf制作一个简单的Spring Boot应用程序。我有一个上传图片的控制器,它保存在项目文件夹 uploads 中。能否请人向我解释,上传后如何立即显示?我应该使用@PostMapping还是仅使用Thymeleaf标签?

我的控制器课程

@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模板。至此,它正在工作。图片已保存在项目中,但现在我想像截图 want it to be like that一样看到这张图片

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)