SpringBoot访问外部文件及默认路由 发表于 2019-06-15 | 分类于 springboot,SpringBoot技能大全 1 新增配置类1234567891011121314151617181920212223242526272829303132package com.pibigstar.common.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import com.pibigstar.common.Constant;@Configurationpublic class WebConfig implements WebMvcConfigurer{ /** * 访问外部文件配置,访问D盘下文件 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //配置server虚拟路径,handler为jsp中访问的目录,locations为files相对应的本地路径 registry.addResourceHandler("/files/**").addResourceLocations("file:///D:upload/"); } /** * 配置默认路由 */ @Override public void addViewControllers(ViewControllerRegistry registry) { //将浏览器的默认行为重定向到主页 registry.addViewController("/").setViewName("redirect:/index.htm"); //测试页面 registry.addViewController("/test.htm").setViewName("/test.jsp"); }} 2 访问我们将test.jpg文件上传到D盘的upload文件夹后,那么在页面端访问则通过:localhost:8080/files/test.jpg -------------本文结束感谢您的阅读-------------