|
@@ -2,24 +2,26 @@ package com.example.fms.service;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
-import com.aliyun.oss.ClientException;
|
|
|
|
-import com.aliyun.oss.OSS;
|
|
|
|
-import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
-import com.aliyun.oss.OSSException;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.aliyun.oss.*;
|
|
|
|
+import com.aliyun.oss.internal.Mimetypes;
|
|
import com.aliyun.oss.model.*;
|
|
import com.aliyun.oss.model.*;
|
|
|
|
+import com.example.fms.common.core.base.RedisConf;
|
|
import com.example.fms.common.model.dto.DownloadSysFileDTO;
|
|
import com.example.fms.common.model.dto.DownloadSysFileDTO;
|
|
import com.example.fms.common.model.dto.SystemFileDTO;
|
|
import com.example.fms.common.model.dto.SystemFileDTO;
|
|
import com.example.fms.common.model.vo.ConfigSettingVO;
|
|
import com.example.fms.common.model.vo.ConfigSettingVO;
|
|
import com.example.fms.common.utils.FileUtils;
|
|
import com.example.fms.common.utils.FileUtils;
|
|
|
|
+import com.example.fms.common.utils.JsonUtils;
|
|
|
|
+import com.example.fms.common.utils.RedisUtil;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.tomcat.jni.OS;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -30,6 +32,8 @@ import java.util.List;
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
public class OssService {
|
|
public class OssService {
|
|
|
|
+ private final FileUtils fileUtils;
|
|
|
|
+ private final RedisUtil redisUtil;
|
|
|
|
|
|
//创建桶
|
|
//创建桶
|
|
public void createBucket() throws Exception {
|
|
public void createBucket() throws Exception {
|
|
@@ -82,7 +86,6 @@ public class OssService {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
public void deleteBucket() throws Exception {
|
|
public void deleteBucket() throws Exception {
|
|
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
|
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
|
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
|
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
|
@@ -121,6 +124,7 @@ public class OssService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* OSS下载文件
|
|
* OSS下载文件
|
|
|
|
+ *
|
|
* @param filePath
|
|
* @param filePath
|
|
* @param configSettingVO
|
|
* @param configSettingVO
|
|
* @return
|
|
* @return
|
|
@@ -143,7 +147,7 @@ public class OssService {
|
|
byte[] buffer = new byte[1024];
|
|
byte[] buffer = new byte[1024];
|
|
int len;
|
|
int len;
|
|
while ((len = ossObject.getObjectContent().read(buffer)) != -1) {
|
|
while ((len = ossObject.getObjectContent().read(buffer)) != -1) {
|
|
- outputStream.write(buffer, 0 , len);
|
|
|
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
}
|
|
}
|
|
} catch (OSSException oe) {
|
|
} catch (OSSException oe) {
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
@@ -167,6 +171,7 @@ public class OssService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除文件
|
|
* 删除文件
|
|
|
|
+ *
|
|
* @param filePath
|
|
* @param filePath
|
|
* @param configSettingVO
|
|
* @param configSettingVO
|
|
* @throws Exception
|
|
* @throws Exception
|
|
@@ -203,6 +208,7 @@ public class OssService {
|
|
|
|
|
|
/**
|
|
/**
|
|
* OSS上传文件
|
|
* OSS上传文件
|
|
|
|
+ *
|
|
* @param directory
|
|
* @param directory
|
|
* @param multipartFile
|
|
* @param multipartFile
|
|
* @param configSettingVO
|
|
* @param configSettingVO
|
|
@@ -261,4 +267,154 @@ public class OssService {
|
|
return systemFileDTO;
|
|
return systemFileDTO;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public OSS getOssClient(ConfigSettingVO configSettingVO) {
|
|
|
|
+ String endpoint = configSettingVO.getEndPoint();
|
|
|
|
+ String accessKeyId = configSettingVO.getAccessKeyId();
|
|
|
|
+ String accessKeySecret = configSettingVO.getAccessKeySecret();
|
|
|
|
+ // 创建OSSClient实例。
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
+ return ossClient;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String setUploadId(String md5, OSS ossClient, InitiateMultipartUploadRequest initiateMultipartUploadRequest) {
|
|
|
|
+ InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
|
|
|
|
+ String uploadId = upresult.getUploadId();
|
|
|
|
+ redisUtil.set(md5, uploadId);
|
|
|
|
+ return uploadId;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getUploadId(String md5) {
|
|
|
|
+ String uploadId = redisUtil.get(md5);
|
|
|
|
+ return uploadId;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void uploadChucks(File file, ConfigSettingVO configSettingVO, Integer index, String md5) throws Exception {
|
|
|
|
+ // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
|
|
|
+ String endpoint = configSettingVO.getEndPoint();
|
|
|
|
+ // 填写Bucket名称,例如examplebucket。
|
|
|
|
+ String bucketName = configSettingVO.getBucketName();
|
|
|
|
+
|
|
|
|
+ String accessKeyId = configSettingVO.getAccessKeyId();
|
|
|
|
+ String accessKeySecret = configSettingVO.getAccessKeySecret();
|
|
|
|
+
|
|
|
|
+ String directory = fileUtils.getDirectoryName();
|
|
|
|
+
|
|
|
|
+ String tempFileName = file.getName();
|
|
|
|
+ // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
|
|
|
+ String objectName = "zhang.XLSX";
|
|
|
|
+ // 创建OSSClient实例。
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 创建InitiateMultipartUploadRequest对象。
|
|
|
|
+ InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
|
|
|
|
+ //上传批次号
|
|
|
|
+ String uploadId = "";
|
|
|
|
+ if (index == 0) {
|
|
|
|
+ uploadId = this.setUploadId(md5, ossClient, request);
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ uploadId = this.getUploadId(md5);
|
|
|
|
+ }
|
|
|
|
+ // 如果需要在初始化分片时设置请求头,请参考以下示例代码。
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 根据文件自动设置ContentType。如果不设置,ContentType默认值为application/oct-srream。
|
|
|
|
+ if (metadata.getContentType() == null) {
|
|
|
|
+ metadata.setContentType(Mimetypes.getInstance().getMimetype(file, objectName));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ UploadPartRequest uploadPartRequest = new UploadPartRequest();
|
|
|
|
+ uploadPartRequest.setBucketName(bucketName);
|
|
|
|
+ uploadPartRequest.setKey(objectName);
|
|
|
|
+ uploadPartRequest.setUploadId(uploadId);
|
|
|
|
+ // 设置上传的分片流。
|
|
|
|
+ // 以本地文件为例说明如何创建FIleInputstream,并通过InputStream.skip()方法跳过指定数据。
|
|
|
|
+
|
|
|
|
+ InputStream instream = new FileInputStream(file);
|
|
|
|
+ uploadPartRequest.setInputStream(instream);
|
|
|
|
+ // 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出此范围,OSS将返回InvalidArgument错误码。
|
|
|
|
+ uploadPartRequest.setPartNumber(index+1);
|
|
|
|
+ // 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
|
|
|
|
+ UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
|
|
|
|
+ // 每次上传分片之后,OSS的返回结果包含PartETag。PartETag将被保存在partETags中。
|
|
|
|
+ // partETags是PartETag的集合。PartETag由分片的ETag和分片号组成。
|
|
|
|
+ List<PartETag> partETags = new ArrayList<PartETag>();
|
|
|
|
+ partETags.add(uploadPartResult.getPartETag());
|
|
|
|
+ this.addPartETags(md5,uploadPartResult.getPartETag());
|
|
|
|
+
|
|
|
|
+ } catch (OSSException oe) {
|
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
|
+ System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
|
+ System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
|
+ System.out.println("Request ID:" + oe.getRequestId());
|
|
|
|
+ System.out.println("Host ID:" + oe.getHostId());
|
|
|
|
+ } catch (ClientException ce) {
|
|
|
|
+ System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
|
+ + "such as not being able to access the network.");
|
|
|
|
+ System.out.println("Error Message:" + ce.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+// if (ossClient != null) {
|
|
|
|
+// ossClient.shutdown();
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getPartETagRedisKey(String md5) {
|
|
|
|
+ String key = RedisConf.OSS_PART_TAG + RedisConf.SYMBOL_COLON + md5;
|
|
|
|
+ return key;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<PartETag> getPartETags(String md5) {
|
|
|
|
+
|
|
|
|
+ List<PartETag> partETags = new ArrayList<>();
|
|
|
|
+ String key = this.getPartETagRedisKey(md5);
|
|
|
|
+ String json = redisUtil.get(key);
|
|
|
|
+ if (json != null) {
|
|
|
|
+ partETags = JSONArray.parseArray(json,PartETag.class);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return partETags;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<PartETag> addPartETags(String md5, PartETag partETag) {
|
|
|
|
+ List<PartETag> partETags = new ArrayList<>();
|
|
|
|
+ String key = this.getPartETagRedisKey(md5);
|
|
|
|
+ String json = redisUtil.get(key);
|
|
|
|
+ if (json != null) {
|
|
|
|
+ partETags = JsonUtils.jsonToList(json, PartETag.class);
|
|
|
|
+ }
|
|
|
|
+ partETags.add(partETag);
|
|
|
|
+ String jsons = JSONObject.toJSONString(partETags);
|
|
|
|
+ redisUtil.set(key,jsons);
|
|
|
|
+ return partETags;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void uploadChunksMerge(ConfigSettingVO configSettingVO, String md5) throws Exception {
|
|
|
|
+ String directory = fileUtils.getDirectoryName();
|
|
|
|
+ OSS ossClient = this.getOssClient(configSettingVO);
|
|
|
|
+ String bucketName = configSettingVO.getBucketName();
|
|
|
|
+ String uploadId = redisUtil.get(md5);
|
|
|
|
+ String tempFileName = IdUtil.simpleUUID() + "." + "xls";
|
|
|
|
+ String objectName = "zhang.XLSX";
|
|
|
|
+ List<PartETag> partETags = this.getPartETags(md5);
|
|
|
|
+ // 创建CompleteMultipartUploadRequest对象。
|
|
|
|
+ // 在执行完成分片上传操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
|
|
|
|
+ CompleteMultipartUploadRequest completeMultipartUploadRequest =
|
|
|
|
+ new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);
|
|
|
|
+
|
|
|
|
+ // 完成分片上传。
|
|
|
|
+ CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
|
|
|
|
+ System.out.println(completeMultipartUploadResult.getETag());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|