Asish Samantaray
Asish Samantaray

Reputation: 11

How to refresh a folder using java program in intellij / Eclipse?

I am currently working on a Spring Boot project while uploading images the image get created in resources\static\images but when I'm trying to display the image it is not showing. After I refresh the folder it get reflected. Here is my code:

// Method for uploading image.
    public void uploadImage(MultipartFile file) {
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedOutputStream stream = null;
        try {
            stream = new BufferedOutputStream(new FileOutputStream(new File(
                    "D:\\New Language\\Spring Boot Demo\\employee_details\\src\\main\\resources\\static\\images"
                            + File.separator + file.getOriginalFilename())));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            stream.write(bytes);
            stream.flush();
            stream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


// JSP Code For displaying image.   
    <div class="card-header border-0">
        <img src="/images/${emp.path}" alt="My Image">
    </div>                                                   

Upvotes: 1

Views: 2424

Answers (2)

Asish Samantaray
Asish Samantaray

Reputation: 11

So finally after 3 days I found the solution to this problem. Since resources\static\images\ place is about static content and since it is in general a bad idea to save uploaded (dynamic) content inside your application. So I created a folder outside of resources folder and put all the uploaded (dynamic) images files.

Image

Here is how I solved this problem. Create a new class and try this.

@Configuration
public class ResourceWebConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
    }
}

Upvotes: 0

GeertPt
GeertPt

Reputation: 17844

You shouldn't change it in the source folder. I'm not 100% sure, but I think IntelliJ will use .../target/classes/ as classpath, and will copy files there during compile. Spring Boot will load any /static folder it finds on the classpath. So you could overwrite files there instead of under .../src/main/resources. That will work until IntelliJ decides to overwrite them during a compile or doing a mvn clean install.

Also, if you run the spring boot app standalone, the resources will be inside a jar file, so that's not a good idea to use as dynamic storage.

Better to create a separate folder for dynamic storage, and configure it as follows:

spring.resources.static-locations=classpath:/static/,file:/D:/...

Off course, if you update that folder at runtime, it's not really static anymore. Check also https://www.baeldung.com/spring-mvc-static-resources

Upvotes: 1

Related Questions