|
|
@@ -147,4 +147,70 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void uploadFolder(String dataSource, String buildResult) {
|
|
|
+ uploadDirectory(ossClient,bucket,dataSource,buildResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void uploadDirectory(OSS ossClient,String bucket,String filePath,String ossPath) {
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(file.isDirectory()){
|
|
|
+ uploadDirectoryContents(ossClient,bucket,file.getPath(),ossPath );
|
|
|
+ }else {
|
|
|
+ upload(ossClient,bucket,file.getPath(),ossPath);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("上传失败:{}",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void uploadDirectoryContents(OSS ossClient,String bucket ,String filePath, String ossPath) {
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ if (files == null || files.length == 0) {
|
|
|
+ log.info("跳过空目录:{}", file.getPath());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (File file1 : files) {
|
|
|
+ if(file1.isDirectory()){
|
|
|
+ uploadDirectoryContents(ossClient,bucket,file1.getPath(),ossPath +"/"+file1.getName());
|
|
|
+ }else {
|
|
|
+ upload(ossClient,bucket,file1.getPath(),ossPath +"/"+file1.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("上传失败:{}",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void upload(OSS ossClient,String bucket,String filePath, String key1) {
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ }
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
+ }
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|