Spring boot load image after upload

泪湿孤枕 提交于 2020-02-01 08:00:06

问题


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.


回答1:


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

file: your fixed path!

Now your catalog like this:

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

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




回答2:


The static directory is loaded at startup. So when you upload images or make changes to any files or folder under the Static folder that will not reflect as ApplicationContext is already initialized.

To upload dynamic image you can follow these steps:

Step 1: Create a directory outside Static folder say media I created on server

/var/www/html/myapp/media

Step 2: Upload files in media directory.

Step 3: Implement WebMvcConfigurer.

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/media/**").addResourceLocations("file:media/");
  }
}

Once uploaded you can access your file http://localhost:8080/media/filename.jpg dynamically.




回答3:


Just consider that war, jar, ear -> briefly saying -> deployed Java applications are archive file and archive file are not changeable after creation. In our case it means that when you are saving any resource in your path it is loading to application path which is not builded yet (Project)... But application which you are trying get resource are builded archive file which is not exist requested resource yet. After restarting application are building again and creates new archive file -> that's why it is visible now ;))). The solution is as answered before ->

  1. just save resource outside of your project and
  2. give access to your application read/write permission on this path.
  3. use absolute path.


来源:https://stackoverflow.com/questions/39464942/spring-boot-load-image-after-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!