[Java] The image is uploaded, but it does not appear on the screen! ?

less than 1 minute read

trouble

Upload images using Spring Boot. Immediately after uploading, the image is not displayed on the HTML, it will be displayed when you wait about 30 seconds and refresh the screen.

How to upload images

Image files -> /resource/static/images/ File name (file name such as xxx-20200712151409.jpg) -> Store in DB

Solution

  1. Create a folder (probably images) under the root folder This time, images are created directly under the root folder called Ramen Kingdom. ![Screenshot 2020-07-12 15.17.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/577071/7ecb1f23-36ef-4c78-cc04-(9b5ee36063db.png)

  2. Create WebConfiguration class (provisionally, AdditionalResourceWebConfiguration)

@Configuration
public class AdditionalResourceWebConfiguration implements WebMvcConfigurer {

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

After that, just change the code that stored the image to images directly under the root folder. However, in HTML, you need to do the following.


<img th:src="@{'/images/' + ${picture.filepath}}"/>

Summary

Files that change dynamically like this time are not stored under static! !! !!

that’s all. Thank you for reading to the end.