您的位置:首页 > 运维架构 > Tomcat

通过tomcat访问磁盘上的文件

2019-08-20 16:10 1406 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_40656662/article/details/99853914

这种方式适用于ssm

springboot 需要配置web文件

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
//WebMvcConfigurerAdapter 过时,用WebMvcConfigurer代替
@Autowired
private CheckLogin checkLogin;
//配置默认启动页面
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

// 这个方法是用来配置静态资源的,比如html,js,css,等等
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceLocations指的是文件放置的目录,addResoureHandler指的是对外暴露的访问路径
//war包方式,无法使用为tomcat配置虚拟目录的方式,需为springboot内置tomcat设置虚拟目录(注意访问路径最好不要和项目中存放静态资源的包名或控制器访问路径相同,容易出错)。
registry.addResourceHandler("/uploadFile/mp/**").addResourceLocations("file:C:/**/uploadFile/mp/");
例:"/upload/**"也能访问到磁盘下文件
registry.addResourceHandler("/uploadFile/video/**").addResourceLocations("file:C:/**/uploadFile/video/");
}

// 这个方法用来注册拦截器,我们自己写好的拦截器需要通过这里添加注册才能生效
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns("/**") 表示拦截所有的请求,
// excludePathPatterns("/login",) 表示除了登陆之外,因为登陆页面不需要登陆也可以访问
registry.addInterceptor(checkLogin).addPathPatterns("/**").excludePathPatterns("/user/**","/statics/**","/kaptcha/getKaptcha","/upload/**");
//注意:静态下所有是/statics/**,而不是/statics/。引以为戒
//super.addInterceptors(registry);    //较新Spring Boot的版本中这里可以直接去掉,否则会报错
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐