阿里云oss
https://help.aliyun.com/document_detail/32011.html?spm=a2c4g.84788.0.0.5cd16d31Ws1xbQ
@Value("endpoint")
private String endpoint;
@Value("accessKeyId")
private String accessKeyId;
@Value("accessKeSecret")
private String accessKeSecret;
@Value("bucketName")
private String bucketName;
public String ossUploadFile(MultipartFile multipartFile){
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeSecret);
try {
String originalFilename = multipartFile.getOriginalFilename();
String filename = UUID.randomUUID().toString();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFilename = filename + "." + suffix;
String fileUrl = getDate("yyyy/MM/dd")+newFilename;
ossClient.putObject(bucketName,fileUrl,multipartFile.getInputStream());
return "https://"+bucketName+"."+endpoint+"/"+fileUrl;
} catch (IOException e) {
e.printStackTrace();
return null;
}finally {
ossClient.shutdown();
}
}
======================================
其他自研实现
service层
@Value("${spring.servlet.multipart.location}")
private String uploadFolder; //这不是tomcat服务目录,别人不认识
@Value("${spring.servlet.multipart.max-file-size}")
private int size;
public Map<String, Object> uploadFile(MultipartFile multipartFile,String path) throws Exception {
if (multipartFile == null) {
throw new Exception("文件为空请重新上传");
}
long size = multipartFile.getSize();
if (size > this.size) {
throw new Exception("文件过大上传失败");
}
//指定文件格式 后期可以变更
String originalFilename = multipartFile.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//文件名
String originalFilename = UUID.randomUUID().toString() + suffix;
if (suffix==null||(!suffix.equals("xlsx")||!suffix.equals("xls"))) {
throw new Exception("文件异常上传失败");
}
//指定文件上传目录
String date = getDate("yyyy/MM/dd");//日期目录
File newFile = new File(uploadFolder + path , date );
try {
//目录不存在
if (!newFile.exists()) newFile.mkdirs();
//文件
File newFileName = new File(newFile, originalFilename);
//文件上传到指定的目录
multipartFile.transferTo(newFileName);
Map<String, Object> map = new HashMap<>();
map.put("url",path +"/" + date +"/" + originalFilename);
map.put("size",size);
map.put("ext",suffix);
map.put("filename",originalFilename);
return map;
} catch (IOException e) {
return null;
}
}
private static String getDate(String pattern){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(new Date());
}
mvc 拦截器配置类
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Value("${file.staticPatternPath}")
private String staticPatternPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("访问的路径").addResourceLocations("上传资源的路径");
registry.addResourceHandler(staticPatternPath)
.addResourceLocations(uploadFolder);
}
}
配置文件中
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=20MB
spring.servlet.multipart.location=""
file.staticPatternPath="/upload/**" #存放路径
file.uploadFolder="/www/upload/"