|
@@ -9,11 +9,6 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
-import com.aliyun.oss.ClientException;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.OSSException;
|
|
|
-import com.aliyun.oss.model.GetObjectRequest;
|
|
|
-import com.aliyun.oss.model.OSSObject;
|
|
|
/**
|
|
|
* @author abnerhou
|
|
|
* @date 2020/5/28 10:37
|
|
@@ -24,28 +19,29 @@ import com.aliyun.oss.model.OSSObject;
|
|
|
public class OssCheckPointUploadUtil {
|
|
|
|
|
|
@Value("${oss.point}")
|
|
|
- private String endpoint;
|
|
|
+ private String endpoint;
|
|
|
|
|
|
@Value("${oss.key}")
|
|
|
- private String accessKeyId;
|
|
|
+ private String accessKeyId;
|
|
|
|
|
|
@Value("${oss.secrey}")
|
|
|
- private String accessKeySecret;
|
|
|
+ private String accessKeySecret;
|
|
|
|
|
|
@Value("${oss.bucket}")
|
|
|
- private String bucketName;
|
|
|
- public void doUploadThenDelete(String uploadFile , String objectName){
|
|
|
+ private String bucketName;
|
|
|
+
|
|
|
+ public void doUploadThenDelete(String uploadFile, String objectName) {
|
|
|
// 创建OSSClient实例。
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
try {
|
|
|
File localFile = new File(uploadFile);
|
|
|
- log.info("Object name = {}" ,objectName);
|
|
|
+ log.info("Object name = {}", objectName);
|
|
|
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, objectName);
|
|
|
// The local file to upload---it must exist.
|
|
|
// 指定上传的本地文件。
|
|
|
uploadFileRequest.setUploadFile(uploadFile);
|
|
|
- log.info("本地文件为:{}" ,uploadFile);
|
|
|
+ log.info("本地文件为:{}", uploadFile);
|
|
|
// Sets the concurrent upload task number to 5.
|
|
|
uploadFileRequest.setTaskNum(5);
|
|
|
// Sets the part size to 1MB.
|
|
@@ -97,40 +93,40 @@ public class OssCheckPointUploadUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String downloadFileFromOss(String localFilePath,String newFileName,
|
|
|
- String bucketName , String objectKey) throws IOException {
|
|
|
- if(StringUtils.isNoneBlank(localFilePath , bucketName , objectKey)){
|
|
|
+ public String downloadFileFromOss(String localFilePath, String newFileName,
|
|
|
+ String bucketName, String objectKey) throws IOException {
|
|
|
+ if (StringUtils.isNoneBlank(localFilePath, bucketName, objectKey)) {
|
|
|
// 创建OSSClient实例。
|
|
|
-// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
- OSS ossClient = new OSSClientBuilder().build("http://oss-cn-shenzhen.aliyuncs.com"
|
|
|
- , "LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4");
|
|
|
+/* OSS ossClient = new OSSClientBuilder().build("http://oss-cn-shenzhen.aliyuncs.com"
|
|
|
+ , "LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4");*/
|
|
|
|
|
|
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
- OSSObject ossObject = ossClient.getObject(new GetObjectRequest( bucketName, objectKey));
|
|
|
+ OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, objectKey));
|
|
|
String fileName = "";
|
|
|
- if(StringUtils.isNotBlank(newFileName)){
|
|
|
+ if (StringUtils.isNotBlank(newFileName)) {
|
|
|
fileName = newFileName;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
int index = objectKey.lastIndexOf("/");
|
|
|
- if(index != -1){
|
|
|
- fileName = objectKey.substring(index + 1 , objectKey.length());
|
|
|
+ if (index != -1) {
|
|
|
+ fileName = objectKey.substring(index + 1, objectKey.length());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
File directory = new File(localFilePath);
|
|
|
- if(!directory.exists()){
|
|
|
+ if (!directory.exists()) {
|
|
|
directory.mkdirs();
|
|
|
}
|
|
|
localFilePath += fileName;
|
|
|
- if(null != ossObject){
|
|
|
+ if (null != ossObject) {
|
|
|
File file = new File(localFilePath);
|
|
|
- if(file.exists()){
|
|
|
+ if (file.exists()) {
|
|
|
FileUtils.deleteFile(localFilePath);
|
|
|
file = new File(localFilePath);
|
|
|
}
|
|
|
InputStream inputStream = ossObject.getObjectContent();
|
|
|
- if(null == inputStream){
|
|
|
+ if (null == inputStream) {
|
|
|
log.error("获取oss返回的输入流失败");
|
|
|
return localFilePath;
|
|
|
}
|
|
@@ -148,8 +144,8 @@ public class OssCheckPointUploadUtil {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
- if(null != stream){
|
|
|
+ } finally {
|
|
|
+ if (null != stream) {
|
|
|
stream.flush();
|
|
|
stream.close();
|
|
|
inputBuff.close();
|
|
@@ -159,23 +155,51 @@ public class OssCheckPointUploadUtil {
|
|
|
}
|
|
|
// 关闭OSSClient。
|
|
|
ossClient.shutdown();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
log.error("入参为空,无法下载oss的照片");
|
|
|
}
|
|
|
return localFilePath;
|
|
|
}
|
|
|
|
|
|
+ public String downLoadOssFileWithSdk(String localDirectory, String newFileName,
|
|
|
+ String bucketName, String objectKey) {
|
|
|
+ if (StringUtils.isNoneBlank(localDirectory, bucketName, objectKey)) {
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ /* OSS ossClient = new OSSClientBuilder().build("http://oss-cn-shenzhen.aliyuncs.com"
|
|
|
+ , "LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4");*/
|
|
|
+ String fileName = "";
|
|
|
+ if (StringUtils.isNotBlank(newFileName)) {
|
|
|
+ fileName = newFileName;
|
|
|
+ } else {
|
|
|
+ int index = objectKey.lastIndexOf("/");
|
|
|
+ if (index != -1) {
|
|
|
+ fileName = objectKey.substring(index + 1, objectKey.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ File directory = new File(localDirectory);
|
|
|
+ if (!directory.exists()) {
|
|
|
+ //递归生成目录
|
|
|
+ directory.mkdirs();
|
|
|
+ }
|
|
|
+ String localFilePath = localDirectory + fileName;
|
|
|
+ File totalFile = new File(localFilePath);
|
|
|
+ // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
+ ossClient.getObject(new GetObjectRequest(bucketName, objectKey), totalFile);
|
|
|
+ // 关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ return localFilePath;
|
|
|
+ }
|
|
|
+ return localDirectory;
|
|
|
+ }
|
|
|
|
|
|
- /* public static void main(String[] args){
|
|
|
|
|
|
- try {
|
|
|
- downloadFileFromOss("E:\\data\\fdkanfang\\t-sk-93IabtI45\\input_img\\" ,"",
|
|
|
- "4d-tjw" , "4dkanfang/3.jpg");
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- System.out.println("done==");
|
|
|
- }*/
|
|
|
+/* public static void main(String[] args){
|
|
|
|
|
|
+ System.out.println(downLoadOssFileWithSdk("E:\\data\\fdkanfang\\abner\\test\\",
|
|
|
+ "mytest.jpg" , "4dkankan","images/images0EkILn1TL/pan/high/31.jpg"));
|
|
|
+ System.out.println("done==");
|
|
|
+ }*/
|
|
|
|
|
|
-}
|
|
|
+
|
|
|
+ }
|