<!--OSS文件服务--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
再配置一下文件上传大小限制
再 application.yml 中添加如下配置,限制单个文件最大 100M 、文件上传总大小 200M。
spring: servlet: multipart: max-file-size: 100MB max-request-size: 200MB
实现功能有:
- 单个文件上传
- 多个文件上传
- 单个文件删除
- 多个文件删除
1 @Slf4j 2 @Component 3 @AllArgsConstructor 4 public class OssTemplate { 5 6 // 我自己抽的统一文件前缀,communication-resource 7 private final String prefix = "oss.file.prefix"; 8 // 你的 bucket,j3-communication 9 private final String bucket = "oss.file.bucket"; 10 // 你的 bucket 外网访问域名,https://oss-cn-guangzhou.aliyuncs.com 11 private final String endpoint = "oss.file.endpoint"; 12 // 拼接返回url要用的,bucket + 域名 例如: j3-communication.oss-cn-guangzhou.aliyuncs.com 13 private final String bucketHost = "oss.file.bucketHost"; 14 // 这两个就没啥好说的了 15 private final String accessKeyId = "oss.file.accessKeyId"; 16 private final String accessKeySecret = "oss.file.accessKeySecret"; 17 // 生成文件路径用的,根据日期 18 private final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); 19 20 /** 21 * 单个文件上传 22 * @param file 23 * @return 24 */ 25 public String upload(MultipartFile file) { 26 // 路径拼接 27 String dataPath = format.format(new Date()); 28 // 文件名称生成 29 String uuid = UUID.randomUUID().toString().replace("-", ""); 30 String path = prefix + "/" + dataPath + "/" + uuid + file.getOriginalFilename(); 31 try { 32 // 上传 33 ossUpload(bucket, path, file.getInputStream()); 34 } catch (IOException e) { 35 throw new SysException("文件上传失败!"); 36 } 37 // 因为 oss 不会返回访问 url 所以我们自己可以拼接一个: 38 // 协议(https://) + 域名访问地址(j3-communication.oss-cn-guangzhou.aliyuncs.com) + 自己拼接的路径(xxx/xxx/a.jpg) 39 return "https://" + bucketHost + "/" + path; 40 } 41 42 43 /** 44 * 多个文件上传 45 * @param files 46 * @return 47 */ 48 public List<String> upload(MultipartFile[] files) { 49 List<String> usrList = new ArrayList<>(files.length); 50 for (MultipartFile file : files) { 51 usrList.add(upload(file)); 52 } 53 return usrList; 54 } 55 56 57 /** 58 * 具体上传代码 59 * @param bucket backet名称 60 * @param path 路径 61 * @param inputStream 文件流 62 * @return 63 */ 64 private PutObjectResult ossUpload(String bucket, String path, InputStream inputStream) { 65 OSS ossClient = null; 66 PutObjectResult putObjectResult = null; 67 try { 68 // 创建OSSClient实例。 69 ossClient = new OSSClientBuilder().build(endpoint 70 , accessKeyId 71 , accessKeySecret); 72 // 通过文件流的形式上传文件 73 putObjectResult = ossClient.putObject(bucket, path, inputStream); 74 } catch (Exception e) { 75 //错误处理 76 log.error("==========ossUpload_error, {}", e); 77 throw new SysException("文件上传失败!"); 78 } finally { 79 //资源关闭 80 assert ossClient != null; 81 ossClient.shutdown(); 82 } 83 return putObjectResult; 84 } 85 86 87 /** 88 * 单个删除 89 * @param url 文件url 90 */ 91 public void delete(String url) { 92 // 处理 url 93 log.info("============入参:{}", url); 94 String path = url.substring(("https://" + bucketHost + "/").length()); 95 log.info("============path:{}", path); 96 ossDelete(bucket, Collections.singletonList(path)); 97 } 98 99 /** 100 * 多个删除 101 * @param urlList 102 */ 103 public void delete(List<String> urlList) { 104 List<String> keys = new ArrayList<>(urlList.size()); 105 for (String url : urlList) { 106 keys.add(url.substring(("https://" + bucketHost + "/").length())); 107 } 108 ossDelete(bucket, keys); 109 } 110 111 /** 112 * 具体删除代码 113 * @param bucket backet 114 * @param pathList 文件url列表 115 */ 116 private void ossDelete(String bucket, List<String> pathList) { 117 OSS ossClient = null; 118 try { 119 // 创建OSSClient实例。 120 ossClient = new OSSClientBuilder().build(endpoint 121 , accessKeyId 122 , accessKeySecret); 123 // ossClient.deleteObject(bucket, path); 124 DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket); 125 deleteObjectsRequest.setKeys(pathList); 126 ossClient.deleteObjects(deleteObjectsRequest); 127 } catch (Exception e) { 128 //错误处理 129 log.error("==========ossUpload_error, {}", e); 130 throw new SysException("文件删除失败!"); 131 } finally { 132 //资源关闭 133 assert ossClient != null; 134 ossClient.shutdown(); 135 } 136 } 137 138 }
2.3 编写 controller 测试
1 @Slf4j 2 @AllArgsConstructor 3 @ResponseResult 4 @RequestMapping("/test/oss") 5 public class OssTemplateTest { 6 7 private final OssTemplate ossTemplate; 8 9 @PostMapping("/upload") 10 public String upload(MultipartFile file) { 11 return ossTemplate.upload(file); 12 } 13 14 @PostMapping("/batch") 15 public List<String> upload(MultipartFile[] files) { 16 return ossTemplate.upload(files); 17 } 18 19 @GetMapping("/delete") 20 public void delete(@RequestParam(name = "url") String url) { 21 ossTemplate.delete(url); 22 } 23 24 @PostMapping("/deletes") 25 public void deletes(@RequestBody List<String> urlList) { 26 ossTemplate.delete(urlList); 27 } 28 29 }
标签:String,url,oss,bucket,file,path,完整版,上传,下载 From: https://www.cnblogs.com/liyongliangs/p/17995003