|
@@ -0,0 +1,125 @@
|
|
|
+package com.ygj.yuemum.utils;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.experimental.Accessors;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class OssService {
|
|
|
+ String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ String accessKeyId = "02yUhyzfxsxzLCKA";
|
|
|
+ String accessKeySecret = "6Cw3uKQKKRedocMtfZVfCxHoeZh2h6";
|
|
|
+ String bucket = "yueguanjia-bucket";
|
|
|
+ String rootserver = "oss-cn-hangzhou.aliyuncs.com";
|
|
|
+
|
|
|
+
|
|
|
+ public UploadDto upload(File file, String folder) throws Exception {
|
|
|
+ OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ objectMetadata.setContentLength(file.length());
|
|
|
+ FileInputStream inputStream = new FileInputStream(file);
|
|
|
+
|
|
|
+ String extension = getExtension(file.getName());
|
|
|
+ String saveName = UUID.randomUUID().toString() + "." + extension;
|
|
|
+
|
|
|
+ String key = folder + "/" + saveName;
|
|
|
+ ossClient.putObject(bucket, key, inputStream, objectMetadata);
|
|
|
+ ossClient.shutdown();
|
|
|
+
|
|
|
+ UploadDto uploadDto = new UploadDto();
|
|
|
+ uploadDto.url = getUrl(key);
|
|
|
+ uploadDto.name = file.getName();
|
|
|
+ uploadDto.saveName = saveName;
|
|
|
+ uploadDto.size = file.length();
|
|
|
+ uploadDto.key = key;
|
|
|
+ return uploadDto;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UploadDto upload(MultipartFile file, String folder) throws Exception {
|
|
|
+ OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ objectMetadata.setContentLength(file.getSize());
|
|
|
+
|
|
|
+
|
|
|
+ String extension = getExtension(file.getOriginalFilename());
|
|
|
+ String saveName = UUID.randomUUID().toString() + "." + extension;
|
|
|
+
|
|
|
+ String key = folder + "/" + saveName;
|
|
|
+ ossClient.putObject(bucket, key, file.getInputStream(), objectMetadata);
|
|
|
+ ossClient.shutdown();
|
|
|
+
|
|
|
+ UploadDto uploadDto = new UploadDto();
|
|
|
+ uploadDto.url = getUrl(key);
|
|
|
+ uploadDto.name = file.getOriginalFilename();
|
|
|
+ uploadDto.saveName = saveName;
|
|
|
+ uploadDto.size = file.getSize();
|
|
|
+ uploadDto.key = key;
|
|
|
+ return uploadDto;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UploadDto upload(InputStream inputStream, String folder, String fileName, long lenth) throws UnsupportedEncodingException {
|
|
|
+ OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+
|
|
|
+ String extension = getExtension(fileName);
|
|
|
+ String saveName = UUID.randomUUID().toString() + "." + extension;
|
|
|
+
|
|
|
+ String key = folder + "/" + saveName;
|
|
|
+ ossClient.putObject(bucket, key, inputStream);
|
|
|
+ ossClient.shutdown();
|
|
|
+
|
|
|
+ UploadDto uploadDto = new UploadDto();
|
|
|
+ uploadDto.url = getUrl(key);
|
|
|
+ uploadDto.name = fileName;
|
|
|
+ uploadDto.saveName = saveName;
|
|
|
+ uploadDto.size = lenth;
|
|
|
+ uploadDto.key = key;
|
|
|
+ return uploadDto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getExtension(String fileName) {
|
|
|
+ int index = fileName.lastIndexOf(".");
|
|
|
+ if (index < 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return fileName.substring(index + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getUrl(String key) throws UnsupportedEncodingException {
|
|
|
+ return "https://" + bucket + "." + rootserver + "/" + URLEncoder.encode(key, "UTF-8");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ @Accessors(chain = true)
|
|
|
+ public static class UploadDto {
|
|
|
+ /**
|
|
|
+ * oss存储地址
|
|
|
+ */
|
|
|
+ private String url;
|
|
|
+ /**
|
|
|
+ * osskey
|
|
|
+ */
|
|
|
+ private String key;
|
|
|
+ private String name;
|
|
|
+ /**
|
|
|
+ * 保存oss文件名称
|
|
|
+ */
|
|
|
+ private String saveName;
|
|
|
+ /**
|
|
|
+ * 文件大小
|
|
|
+ */
|
|
|
+ private Long size;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|