原因:因为对服务器的保护措施导致的,服务器不能对外部暴露真实的资源路径,需要配置虚拟路径映射访问。
解决方法:
1、首先配置好spring boot目录下的application.yml,相关配置如下。
(spring boot application.yml读取static文件夹中的css、js、images等的配置)
2、建立ImagesUploadConfig类,实现WebMvcConfigurer接口。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ImagesUploadConfig implements WebMvcConfigurer {
/**
* 解决图片上传无法立即显示,而需要重启才能访问的问题。
* 这是因为对服务器的保护措施导致的,服务器不能对外部暴露真实的资源路径,需要配置虚拟路径映射访问。
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//获取文件的真实路径
String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\";
// /images/**是对应resource下工程目录
registry.addResourceHandler("/images/**").addResourceLocations("file:"+path);
}
}
————————————————
版权声明:本文为CSDN博主「welingfeng1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/welingfeng1/article/details/105370696/