Serving Static resources from file system | Spring Boot Web

前端 未结 4 631
忘了有多久
忘了有多久 2020-12-14 01:58

Using a Spring Boot web application I trying to serve my static resource from a file system folder outside my project.

Folder structure looks like:-

         


        
相关标签:
4条回答
  • 2020-12-14 02:45

    This is what I did in the WebConfig class, inside the addResourceHandlers method:

    boolean devMode = this.env.acceptsProfiles("development");
    String location;
    if (devMode) {
        String currentPath = new File(".").getAbsolutePath();
        location = "file:///" + currentPath + "/client/src/";
    } else {
        location = "classpath:static/";
    }
    
    0 讨论(0)
  • 2020-12-14 02:50

    All solutions provided with Spring Boot 2 didn't work in our case, not with the WebMvcConfigurerAdapter nor with the WebMvcConfigurer. Using the @EnableWebMvc annotation made it even worse as the regular contents that worked before stopped working also due to the fact that the WebMvcAutoConfiguration was getting ignored at that point I suppose.

    One possible solution is to define the spring.resources.static-locations property, but this implies that the static location is hard coded. We wanted to add a static location at runtime so it is independent of the environment we're deploying to as the external directoy containing the resource was located at the same place as where the application is deployed. To achieve this I came up with the following solution:

    @Configuration
    @SpringBootApplication
    public class MainConfiguration {
        @Inject
        public void appendExternalReportingLocation(ResourceProperties resourceProperties) {
            String location = "file://" + new File("ext-resources").getAbsolutePath();
            List<String> staticLocations = newArrayList(resourceProperties.getStaticLocations());
            staticLocations.add(location);
            resourceProperties.setStaticLocations(staticLocations.toArray(new String[staticLocations.size()]));
        }
    }
    

    UPDATE: Unfortunately the above solution only worked when launching the Spring Boot application from within the IDE (e.g. IntelliJ). So I came up with another solution to serve static content from the file system.

    First I created a Filter as follow:

    public class StaticContentFilter implements Filter {
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            File file = new File(new File("ext-resources").getAbsolutePath(), ((HttpServletRequest)request).getServletPath());
            if (file.exists() && !file.isDirectory()) {
                org.apache.commons.io.IOUtils.copy(new FileInputStream(file), response.getOutputStream());
            }
            else {
                chain.doFilter(request, response);
            }
        }
    }
    

    Then I registerd with Spring Boot as follow:

    @Configuration
    @SpringBootApplication
    public class MainConfiguration {
        @Bean
        public FilterRegistrationBean staticContentFilter() {
            return new FilterRegistrationBean(new StaticContentFilter());
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:58

    Spring Boot Maven Plugin can add extra directories to the classpath. In your case you could include that in your pom.

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot.version}</version>
        <configuration>
            <folders>
                <folder>${project.build.directory}/../ext-resources</folder>
            </folders>
    
            ...
        </configuration>
    </plugin>
    

    So that way you don't need inlcude any hard-code in your classes. Simply start your webapp with

    mvn spring-boot:run
    
    0 讨论(0)
  • 2020-12-14 03:03

    file:/// is an absolute URL pointing to the root of the filesystem and, therefore, file:///./ext-resources/ means that Spring Boot is looking for resources in a directory named ext-resources in the root.

    Update your configuration to use something like file:ext-resources/ as the URL.

    0 讨论(0)
提交回复
热议问题