Juliette
Juliette

Reputation: 1026

Java Spring Access Static Resources outside application

I want to enable my users to upload images which are then stored on the server. Since putting those images inside the applications resources/static-folder would remove them with every new deployment I want to store and access those images outside of my Spring application.

I still need to display the images in my html-Context so what I am trying to do is to map a path (e.g. "userimages") to a location outside of my application (e.g. "/home/userimages/" So that when the url localhost:8080/userimages/hi.jpg is called, the image from /home/userimages/hi.jpg is returned.

I found the function addResourceHandlers that seems to be able to do exactly this, but I have trouble implementing it. For starters I am unsure whether to extend WebMvcConfigurationSupport or WebMvcConfigurationAdapter or implement WebMvcConfigurer. None seems to work. I also tried around with different @ above that class like @EnableWebMvc etc. - nothing changed. Some sites suggested moving the Class to the same package as the application - also didn't work.

I think that the function addResourceHandlers is not even called and I don't know how to make sure it is. This is currently my code:

package global;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class StaticResourceProvider implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //This never gets printed
        System.out.print("Adding resource handler");
        registry
                .addResourceHandler("/user-images/**")
                //for Unix: file:/opt/files
                //TODO: use path variable here
                .addResourceLocations("file:///C:/Users/Juliette/Pictures/");
    }
}

And the entry point to my application:

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        //TODO: remove the first array element and read it as the name of this instance
        SpringApplication.run(Application.class, args);
    }

}

Can someone tell me what my mistake is? Currently when I access localhost:8080/user-images/nameOfSomeImage.jpg only a white page is returned and no message shows up in the server log.

Upvotes: 2

Views: 2313

Answers (1)

Juliette
Juliette

Reputation: 1026

I managed to make it work now by moving both classes in the same package and modifying them as follows:

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        //TODO: remove the first array element and read it as the name of this instance
        SpringApplication.run(Application.class, args);
    }

}

And the ResourceProvider:

package controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class StaticResourceProvider implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static/css/");

        registry
                .addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/");

        registry
                .addResourceHandler("/img/**")
                //for Unix: file:/opt/files
                //TODO: use path here
                .addResourceLocations("file:///C:/Users/Juliette/Pictures/");
    }
}

Upvotes: 2

Related Questions