SpringBoot配置项目访问路径URL的根路径,并访问项目本地图片
配置项目跟路径,默认localhost:8080,如添加了项目访问跟路径,上传文件返回的URL也需要包含此路径(没配置就不用写)
1、SpringBoot在2.0之前版本使用server.context-path
server.context-path=/demo
2、SpringBoot在2.0之后版本使用server.servlet.context-path
server.servlet.context-path=/demo
spring:
# 启动环境 Dev|Test|Prod
profiles:
active: Dev
freemarker:
#模板引擎缓存
cache: true
#本地文件上传到的路径
resources:
static-locations: file:E:/demo/uploadPath/
#文件对外暴露的访问路径
mvc:
static-path-pattern: /**
新建一个类,类名随意,实现WebMvcConfigurer 重写addResourceHandlers方法
/**
* WebMvc配置
* @author zhouzhou
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations(staticPrefix)
.setCacheControl(CacheControl.maxAge(cacheTime, TimeUnit.DAYS).cachePublic());/flowable/**").addResourceLocations("classpath:/flowable/");
/** 本地文件上传路径 */
// registry.addResourceHandler("/profile/**").addResourceLocations("file:" + ShopConfig.getProfile(),"file:" + Global.getEditorPath());
registry.addResourceHandler("/profile/**").addResourceLocations("file:" + FileUploadConfig.getProfile());
/** swagger配置 */
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
}
/**
* 注册系统拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(LicenseCheckInterceptor).addPathPatterns("/**")
.excludePathPatterns("/static/**","/authInsu/**", "/profile/**","/image/**");
registry.addInterceptor(securityKeyInterceptorAdapter).addPathPatterns("/**");
registry.addInterceptor(lockHandlerInterceptorAdapter).addPathPatterns("/**");
registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
if(ToolUtil.isNotEmpty(adapterRegistry)){
for(String key: adapterRegistry.keySet()){
registry.addInterceptor(SpringUtil.getBean(key)).addPathPatterns(adapterRegistry.get(key));
}
}
}
}
文件上传工具类参考若依 FileUploadUtils ,FileUploadConfig也可参考,类名随意
http://localhost:8080:输入你自己的ip地址和端口,demo:是我自己项目登录访问路径
/**
* 文件上传
*/
@Controller
@RequestMapping("/uploadFile")
public class UploadFileController {
/**
* @param file
* @return
*/
@PostMapping("/uploadFile")
@ResponseBody
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) {
try
{
// 上传文件路径
String filePath = FileUploadConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = fileName;
Map<String,Object> resultMap = new HashMap<>();
//状态码,成功0,失败1
resultMap.put("code","0");
//提示消息
resultMap.put("msg","上传成功");
//数据(表格填充数据)
resultMap.put("data",url);
//分页总条数
resultMap.put("count",0);
return resultMap;
}
catch (Exception e){
e.printStackTrace();
return error("上传失败");
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107568.html