1、图片上传
public Object localUpload(HttpServletRequest request,String uploadpath){
MultipartHttpServletRequest mReq = (MultipartHttpServletRequest)request;
MultipartFile file = mReq.getFile("file");
String fileName= file.getOriginalFilename();
//重新命名图片文件
fileName = fileUtils.reName(fileName);
File localFile = new File(uploadpath,fileName);
if(!localFile.getParentFile().exists()){ //判断文件父目录是否存在
localFile.getParentFile().mkdir();
}
try {
file.transferTo(localFile);
return "上传成功";
}catch (Exception e){
return "上传失败";
}
}
2、图片访问(资源映射)
配置文件中设置
#访问服务器本地资源
web.upload-path=D:/upload/
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/static/,classpath\:/public/,file\:${web.upload-path} classpath:/static/,classpath:/public/,file:${web.upload-path}
网友评论