tomcat——静态资源映射
rehoni / 2021-12-02
方法一
tomcat访问外部的资源,配置静态资源映射
https://www.cnblogs.com/iyangyuan/p/3316444.html
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" docBase="/users/ems/zhsbdStatic" path="/zhsbdResources" reloadable="true"> </Context>
server.xml的Host标签下
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- 增加虚拟路径,供访问本地图片-->
<Context path="/upload_file" docBase="D:/upload/img" reloadable="false" ></Context>
</Host>
方法二
只适合jar包或者打包了资源的war包,未打包资源的war包丢入服务器的tomcat,是无法通过配置来访问外部资源的
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* Add handlers to serve static resources such as images, js, and, css
* files from specific locations under web application root, the classpath,
* and others.
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// addResourceHandler 服务的相对路径
// addResourceLocations 本机/部署环境的绝对路径,注意Windows下前缀不同 file:///D:/files/
registry.addResourceHandler("/oiisResources/**").addResourceLocations("file:/users/ems/websophic/static/");
}
}