原文链接:https://blog.csdn.net/cumtqt/article/details/81951475
配置虚拟路径 供外部访问图片 视频等文件
1.配置application.properties
##配置虚拟路径映射
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, file:/Users/admin/Downloads/output/
原文链接说其中file:/Users/admin/Downloads/output/ 必须是绝对路径,但我想说。。。相对路径肯定也是可以的。
2.编写configuration.java文件:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @program: appbuilder
* @description:
* @author: seanol
* @create: 2018-12-26 19:50
**/
@Configuration
public class MyWebAppConfiguration extends WebMvcConfigurationSupport {
/**
* @Description:
* 对文件的路径进行配置, 创建一个虚拟路径/Path/**
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/path/**")
.addResourceLocations("file:/Users/admin/Downloads/output/");
super.addResourceHandlers(registry);
}
}
3.重新启动服务
4.测试
访问: http://localhost:8080/path/demo.zip
注意:如果有配置服务名称(context-path)记得加上!