外置tomcat映射路径
在tomcat里的conf下的server.xml里Host标签下加入
<Context docBase="D:\pdf\" path="report" reloadable="true" />
其中的docBase就是磁盘映射路径,path为访问路径,比如localhost:8080/report就可以访问到d盘pdf文件夹下的静态文件
image.png
springboot 1.x内置映射路径方法
继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
重写addResourceHandlers方法,其中report为访问路径,后面为文件映射绝对路径,file开头代表是一个文件位置绝对路径。
package cn.ways.gtids.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import cn.ways.util.PropertiesUtils;
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* 资源映射路径
* addResourceHandler:访问映射路径
* addResourceLocations:资源绝对路径
*/
registry.addResourceHandler("/report/**").addResourceLocations("file:D:/pdf/");
}
}
springboot 2.x内置映射路径方法
实现WebMvcConfigurer
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/report/**").addResourceLocations("file:D:/pdf/");
}
}
springboot默认静态资源访问配置
@Configuration
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
网友评论