直到我刷新页面Spring Mvc才显示上传的图像

Moh*_*aki 7 java eclipse spring-mvc image-uploading thymeleaf

我正在使用带有百万美元的spring mvc将图像上传到服务器并在单独的页面"结果"中提交表单后显示它.

问题是在我刷新Spring Tool Suite中的资源文件夹"resources/static/images"然后刷新页面之前,图像不会显示.我修改了eclipse工作区选项以自动刷新并且ide部分已解决,但仍需要刷新结果页面以显示图像

这是控制器代码

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String contentSubmit(@Valid @ModelAttribute ContentEntity cm, BindingResult result,
        @RequestParam("file") MultipartFile file, Model model) {
    if (!file.isEmpty()) {
        try {
            cm.setImgUrl(file.getOriginalFilename());
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File("src/main/resources/static/images/"+cm.getImgUrl())));
            stream.write(bytes);
            stream.close();
        } catch (Exception e) {
            //return error page
        }
    }

    if (result.hasErrors()) {
        return "cms";
    }

    contentDao.addContent(cm);
    return "view";
}
Run Code Online (Sandbox Code Playgroud)

这是视图代码

<img th:src="@{'images/'+${contentEntity.imgUrl}}" class="image-responsive thumbnail" />
Run Code Online (Sandbox Code Playgroud)

ska*_*dya 5

您正在“ src / resources / ...”目录中实用地创建资源(图像文件)。但是,看来您的服务器是来自不同目录的服务器图像(例如target / **或bin / **)。因此,当您刷新资源目录时,STS会检测到新创建的新文件,然后将其复制到目标目录下。

eclipse的“本机挂钩或轮询”中有一个选项,可以不断监视资源的更改(即使这些更改是在STS / eclipse的外部进行的)。一旦检测到更改,资源将自动刷新。

您可能要通过以下方式设置此选项:首选项>常规>工作区>“使用本机挂钩或轮询刷新”

在此处输入图片说明

希望这可以帮助。

  • 它是否有可能在服务器(生产状态)中也发生,或者这只是本地环境(开发状态)的问题?保存图像/文件的过程安全吗? (2认同)