问题:springboot上传图片后图片无法显示,需要打开一下idea才能访问上传的图片
问题分析:spring boot自带tomcat保护机制,不能直接访问
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
解决办法:
@Configuration
public class ResourceConfigAdapter extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//获取文件的真实路径 work_project代表项目工程名 需要更改
System.out.println(11+System.getProperty("user.dir"));//输出D:\ideaProject\work_project,所以不能重复写work_project
String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\file\\";
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
registry.addResourceHandler("/file/**").
addResourceLocations("file:"+path);
}else{//linux和mac系统 可以根据逻辑再做处理
registry.addResourceHandler("/file/**").
addResourceLocations("file:"+path);
}
super.addResourceHandlers(registry);
}
}
文件目录: