任何项目都可以使用。贴上代码
package Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.ContentVersionStrategy;
import org.springframework.web.servlet.resource.VersionResourceResolver;
@Configuration
public class WebConfig implements WebMvcConfigurer {
private static Logger logger = LoggerFactory.getLogger(WebConfig.class);
@Value("${file.path:E://temp/}")
private String mImagesPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
.addVersionStrategy(new ContentVersionStrategy(), "/**");
// 配置静态文件访问路径
registry
.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(2592000)
.resourceChain(true)
.addResolver(versionResourceResolver);
// 解决swagger无法访问
registry
.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry
.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
// 配置图片或者文件访问路径
if (mImagesPath.equals("") || mImagesPath.equals("${file.path}")) {
String imagesPath = WebConfig.class.getClassLoader().getResource("").getPath();
logger.info("1.上传配置类imagesPath==" + imagesPath);
if (imagesPath.indexOf(".jar") > 0) {
imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
} else if (imagesPath.indexOf("classes") > 0) {
imagesPath = "file:" + imagesPath.substring(0, imagesPath.indexOf("classes"));
}
imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/")) + "/getfile/";
mImagesPath = imagesPath;
}
logger.info("imagesPath=============" + mImagesPath);
registry.addResourceHandler("/getfile/**").addResourceLocations("file:" + mImagesPath);
}
}