本文介绍了上传后的Spring Boot加载映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将图像上传到服务器,并且可以在路径/static/images/gallery中找到我的图像.现在,当我尝试加载上传的图像时,应用程序未显示主题.仅在应用程序重新启动之后.

I was able to upload image to server and I can locate my image in path /static/images/gallery. Now when I try to load uploaded images the application is not displaying theme. Only after application restart.

推荐答案

我遇到了同样的问题!因为静态目录是在启动时加载的!您必须将上传路径放在资源之外!将此项目名称命名为"Demo".我认为您的目录是这样的:

I got the same issues! Because the static directory was loaded in the startup! You must put the upload path outside the resources!Let this project name is "Demo". I think your catalog like this:

Demo
  ∟ src
    ∟ main
      ∟ java
      ∟ resources
        ∟ static
          ∟ images
            ∟ gallery

不要将目录上载到资源中!您可以这样:

Don't put you upload directory in resources!You can do like this:

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/upload/**").addResourceLocations("file://" + System.getProperty("user.dir") + "/src/main/upload/");
  }
}

现在,您的目录如下:

    Demo
      ∟ src
        ∟ main
          ∟ java
          ∟ resources
            ∟ static
              ∟ images
                ∟ gallery
          ∟ upload
            ∟ static
              ∟ images
                ∟ gallery

好!重新启动您的spring boot!尝试上传一些图片!您可以在 http://localhost:80/upload/static/images/gallery/demo.jpg

OK! Restart your spring boot! Try to upload some images! You can see those image in http://localhost:80/upload/static/images/gallery/demo.jpg

这篇关于上传后的Spring Boot加载映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:00