|
@@ -9,7 +9,9 @@ import com.aliyun.oss.model.OSSObject;
|
|
|
import com.aliyun.oss.model.PutObjectResult;
|
|
|
import com.jpsoft.shinestar.config.OSSConfig;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
@@ -17,6 +19,7 @@ import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.zip.ZipEntry;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
@@ -45,8 +48,7 @@ public class OSSUtil {
|
|
|
+ "/" + cal.get(Calendar.DAY_OF_MONTH)
|
|
|
+ "/" + UUID.randomUUID().toString() + "/";
|
|
|
|
|
|
- OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
-
|
|
|
+ OSS ossClient = null;
|
|
|
// int index = fileName.indexOf(".");
|
|
|
// String prefix = fileName.substring(0,index);
|
|
|
// String ext = fileName.substring(index);
|
|
@@ -54,13 +56,57 @@ public class OSSUtil {
|
|
|
// String retFileUrl = savePath + newFileName;
|
|
|
String retFileUrl = savePath + fileName;
|
|
|
|
|
|
- // 上传文件流
|
|
|
- PutObjectResult result = ossClient.putObject(ossConfig.getBucketName(), retFileUrl, fileInputStream);
|
|
|
+ boolean success = false;
|
|
|
|
|
|
- // 关闭OSSClient
|
|
|
- ossClient.shutdown();
|
|
|
+ try {
|
|
|
+ ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+
|
|
|
+ // 上传文件流
|
|
|
+ PutObjectResult putObjectResult = ossClient.putObject(ossConfig.getBucketName(), retFileUrl, fileInputStream);
|
|
|
+
|
|
|
+// putObjectResult.getResponse() is null
|
|
|
+ success = true;
|
|
|
+ }
|
|
|
+ catch (Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ success = false;
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ if(ossClient!=null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String retFullUrl = "";
|
|
|
+
|
|
|
+ if(success) {
|
|
|
+ retFullUrl = ossConfig.getUrlPrefix() + "/" + retFileUrl;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ //保存到本地
|
|
|
+ File folder = new File(ossConfig.getLocalPath() + File.separator + savePath.replaceAll("/", Matcher.quoteReplacement(File.separator)));
|
|
|
+
|
|
|
+ if (!folder.exists()){
|
|
|
+ folder.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ fileInputStream.reset();
|
|
|
+ FileOutputStream output = new FileOutputStream(folder.getAbsolutePath() + File.separator + fileName);
|
|
|
+ StreamUtils.copy(fileInputStream,output);
|
|
|
+ success = true;
|
|
|
+ }
|
|
|
+ catch (Exception ex){
|
|
|
+ success = false;
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (success){
|
|
|
+ retFullUrl = ossConfig.getLocalUrlPrefix() + "/" + retFileUrl;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return ossConfig.getUrlPrefix() + "/" + retFileUrl;
|
|
|
+ return retFullUrl;
|
|
|
}
|
|
|
|
|
|
public static boolean download(String fileUrl,String filePath){
|