spring-boot配置外部静态资源的方法

import java.io.File;

import javax.servlet.Servlet;

import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@Order(Ordered.HIGHEST_PRECEDENCE )
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
@Slf4j
public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*
File directory = new File("");
String path2=directory.getAbsolutePath();
StringBuilder builder = new StringBuilder();
builder.append("file:").append(path2).append("/m/"); //registry.addResourceHandler("/**").addResourceLocations( builder.toString() );
//String myExternalFilePath = "file:///C:/Temp/whatever/m/";
log.info( builder.toString() );
*/ registry.addResourceHandler("/m/**").addResourceLocations("file:m/");
//registry.addResourceHandler("/m/**").addResourceLocations(builder.toString()); super.addResourceHandlers(registry);
}
}
上一篇:Java 多态透析 详细理解


下一篇:Py-解决粘包现象,tcp实现并发,tcp实现传输文件的程序,校验思路,线程与进程