Parcourir la source

Merge remote-tracking branch 'origin/master'

dengsixing il y a 3 ans
Parent
commit
5fc20b46c4

+ 150 - 160
4dkankan-utils-fyun/src/main/java/com/fdkankan/fyun/oss/UploadToOssUtil.java

@@ -78,172 +78,186 @@ public class UploadToOssUtil {
 	@Value("${local.path:/home/4dkankan}")
 	private String localPath;
 
-
-	public void delete(String key1) throws IOException{
-		if("oss".equals(type)){
-			OSSClient ossClient = new OSSClient(point, key, secrey);
-			try {
-				// bucketMgr.delete(bucketname, key);
-				// 2019-2-28 启动aliyun oss 空间
-				ossClient.deleteObject(bucket, key1);
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
+	//上传的数据是byte[],key是上传后的文件名
+	public void upload(byte[] data,String key1) throws IOException{
+		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , data,key1,type);
+		switch (type){
+			case "oss":uploadOss(data,key1); break;
+			case "aws": uploadAws(data,key1); break;
+			case "local":uploadLocal(data,key1);  break;
 		}
-		if("aws".equals(type)){
-			deleteS3Object(key1);
+	}
+	public void upload(String filePath, String key1) {
+		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
+		switch (type){
+			case "oss":uploadOss(filePath,key1); break;
+			case "aws": uploadAws(filePath,key1); break;
+			case "local":uploadLocal(filePath,key1);  break;
 		}
-		if("local".equals(type)){
-			FileUtil.del(key1);
+	}
+	public void uploadSdk(String filePath, String key1) {
+		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
+		switch (type){
+			case "oss":uploadSdkOss(filePath,key1); break;
+			case "aws": uploadAws(filePath,key1); break;
+			case "local":uploadLocal(filePath,key1);  break;
 		}
-
+	}
+	public void upload2(String filePath, String key1) {
+		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
+		switch (type){
+			case "oss":upload2Oss(filePath,key1); break;
+			case "aws": uploadAws(filePath,key1); break;
+			case "local":uploadLocal(filePath,key1);  break;
+		}
+	}
+	public void delete(String key1) throws IOException{
+		switch (type){
+			case "oss":deleteOss(key1); break;
+			case "aws": deleteS3Object(key1); break;
+			case "local":FileUtil.del(key1);  break;
+		}
+	}
+	public int deleteFile(String prefix){
+		switch (type){
+			case "oss":deleteOssFile(prefix); break;
+			case "aws": deleteS3Object(prefix); break;
+			case "local":FileUtil.del(prefix);  break;
+		}
+		return 1;
 	}
 
-	//上传的数据是byte[],key是上传后的文件名
-	public void upload(byte[] data,String key1) throws IOException{
+	public void deleteOss(String key1){
 		OSSClient ossClient = new OSSClient(point, key, secrey);
-		try
-		{
+		try {
+			ossClient.deleteObject(bucket, key1);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
 
-			//bucketMgr.delete(bucketname, key);
-			//Response res = uploadManager.put(data, key, getUpToken(key));
+	public void deleteOssFile(String prefix){
+		OSSClient ossClient = new OSSClient(point, key, secrey);
+		ObjectListing objectListing = ossClient.listObjects(bucket, prefix);
+		List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
+		try {
+			for (OSSObjectSummary s : sums) {
+				delete(s.getKey());
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
 
-			// 2019-2-28 启动aliyun oss 空间
+	public void uploadOss(byte[] data,String key1){
+		OSSClient ossClient = new OSSClient(point, key, secrey);
+		try {
 			ossClient.putObject(bucket, key1, new ByteArrayInputStream(data));
-			//log.info(res.bodyString());
 		} catch (Exception e) {
 			log.error(e.toString()+key1);
 		}
 	}
-
-
-	public void upload(String filePath, String key1) {
-		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
-		if("oss".equals(type)){
-			OSSClient ossClient = new OSSClient(point, key, secrey);
-			try {
-				File file = new File(filePath);
-				if (!file.exists()) {
-					log.error("要上传的文件不存在:" + filePath);
-				}
-				ObjectMetadata metadata = new ObjectMetadata();
-				if(filePath.contains(".jpg")){
-					metadata.setContentType("image/jpeg");
+	public void uploadAws(byte[] data,String key1){
+	}
+	public void uploadLocal(byte[] data,String key1){
+		InputStream in = new ByteArrayInputStream(data);
+		File file = new File(key1);
+		String path = key1.substring(0, key1.lastIndexOf("/"));
+		if (!file.exists()) {
+			new File(path).mkdir();
+		}
+		FileOutputStream fos = null;
+		try {
+			fos = new FileOutputStream(file);
+			int len = 0;
+			byte[] buf = new byte[1024];
+			while ((len = in.read(buf)) != -1) {
+				fos.write(buf, 0, len);
+			}
+			fos.flush();
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			if (null != fos) {
+				try {
+					fos.close();
+				} catch (IOException e) {
+					e.printStackTrace();
 				}
-				ossClient.putObject(bucket, key1, new File(filePath), metadata);
-
-			} catch (Exception e) {
-				log.error(e.toString() + filePath);
 			}
 		}
+	}
 
-		if("aws".equals(type)){
-			try{
-				uploadS3File(filePath, key1);
-			}catch (Exception e){
-				e.printStackTrace();
+
+	public void uploadOss(String filePath, String key1){
+		OSSClient ossClient = new OSSClient(point, key, secrey);
+		try {
+			File file = new File(filePath);
+			if (!file.exists()) {
+				log.error("要上传的文件不存在:" + filePath);
 			}
-		}
-		if("local".equals(type)){
-			try {
-				File srcFile = new File(filePath);
-				File file = new File(localPath + key1);
-				FileUtils.copyFile(srcFile,file);
-			}catch (Exception e){
-				e.printStackTrace();
+			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);
+		}
+	}
+	public void uploadAws(String filePath, String key1){
+		try{
+			uploadS3File(filePath, key1);
+		}catch (Exception e){
+			e.printStackTrace();
+		}
+	}
+	public void uploadLocal(String filePath, String key1){
+		try {
+			File srcFile = new File(filePath);
+			File file = new File(localPath + key1);
+			FileUtils.copyFile(srcFile,file);
+		}catch (Exception e){
+			e.printStackTrace();
 		}
 	}
 
-	public void uploadSdk(String filePath, String key1) {
-		if("oss".equals(type)){
-			OSSClient ossClient = new OSSClient(point, key, secrey);
-			try {
-				File file = new File(filePath);
-				if (!file.exists()) {
-					log.error("要上传的文件不存在:" + filePath);
-				}
-				// 调用put方法上传
-				// Response res = uploadManager.put(FilePath, key, getUpToken(key));
-				// 打印返回的信息
-				// log.info(res.bodyString());
-
-				// 2019-2-28 启动aliyun oss 空间
-//			ObjectMetadata meta = new ObjectMetadata();
-//			meta.setCacheControl("no-cache");
-//			ossClient.putObject(BUCKET_NAME, key, new File(filePath), meta);
-
-				ObjectMetadata metadata = new ObjectMetadata();
-				if(filePath.contains(".jpg")){
-					metadata.setContentType("image/jpeg");
-				}
-				ossClient.putObject(bucketSdk, key1, new File(filePath), metadata);
 
-			} catch (Exception e) {
-				log.error(e.toString() + filePath);
-			}
-		}
-		if("aws".equals(type)){
-			try{
-				uploadS3File(filePath, key1);
-			}catch (Exception e){
-				e.printStackTrace();
+	public void uploadSdkOss(String filePath, String key1){
+		OSSClient ossClient = new OSSClient(point, key, secrey);
+		try {
+			File file = new File(filePath);
+			if (!file.exists()) {
+				log.error("要上传的文件不存在:" + filePath);
 			}
-		}
-		if("local".equals(type)){
-			try {
-				File srcFile = new File(filePath);
-				File file = new File(localPath + key1);
-				FileUtils.copyFile(srcFile,file);
-			}catch (Exception e){
-				e.printStackTrace();
+			ObjectMetadata metadata = new ObjectMetadata();
+			if(filePath.contains(".jpg")){
+				metadata.setContentType("image/jpeg");
 			}
+			ossClient.putObject(bucketSdk, key1, new File(filePath), metadata);
+
+		} catch (Exception e) {
+			log.error(e.toString() + filePath);
 		}
 	}
 
-	public void upload2(String filePath, String key1) {
-		log.info("开始上传文件 源路径:{},目标路径:{},type:{}" , filePath,key1,type);
-		if("oss".equals(type)){
-			OSSClient ossClient = new OSSClient(point, key, secrey);
-			try {
-				// 调用put方法上传
-				// Response res = uploadManager.put(FilePath, key, getUpToken(key));
-				// 打印返回的信息
-				// log.info(res.bodyString());
-
-				// 2019-2-28 启动aliyun oss 空间
-				ObjectMetadata metadata = new ObjectMetadata();
-				if(filePath.contains(".jpg")){
-					metadata.setContentType("image/jpeg");
-				}
-				if(filePath.contains(".mp4")){
-					metadata.setContentType("video/mp4");
-				}
-				if(filePath.contains(".mp3")){
-					metadata.setContentType("audio/mp3");
-				}
-				ossClient.putObject(bucket, key1, new File(filePath), metadata);
-			} catch (Exception e) {
-				log.error(e.toString() + filePath);
+	public void upload2Oss(String filePath, String key1){
+		OSSClient ossClient = new OSSClient(point, key, secrey);
+		try {
+			ObjectMetadata metadata = new ObjectMetadata();
+			if(filePath.contains(".jpg")){
+				metadata.setContentType("image/jpeg");
 			}
-		}
-
-		if("aws".equals(type)){
-			try{
-				uploadS3File(filePath, key1);
-			}catch (Exception e){
-				e.printStackTrace();
+			if(filePath.contains(".mp4")){
+				metadata.setContentType("video/mp4");
 			}
-		}
-
-		if("local".equals(type)){
-			try {
-				File srcFile = new File(filePath);
-				File file = new File(localPath + key1);
-				FileUtils.copyFile(srcFile,file);
-			}catch (Exception e){
-				e.printStackTrace();
+			if(filePath.contains(".mp3")){
+				metadata.setContentType("audio/mp3");
 			}
+			ossClient.putObject(bucket, key1, new File(filePath), metadata);
+		} catch (Exception e) {
+			log.error(e.toString() + filePath);
 		}
 	}
 
@@ -266,27 +280,6 @@ public class UploadToOssUtil {
 		log.info("批量上传文件结束,用时:{}" ,(System.currentTimeMillis() - start));
 	}
 
-	public int deleteFile(String prefix){
-		if("oss".equals(type)){
-			OSSClient ossClient = new OSSClient(point, key, secrey);
-
-			ObjectListing objectListing = ossClient.listObjects(bucket, prefix);
-			List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
-			try {
-				for (OSSObjectSummary s : sums) {
-					delete(s.getKey());
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-
-		}
-
-		if("aws".equals(type)){
-			deleteS3Object(prefix);
-		}
-		return 1;
-	}
 
 
 	public Map<String, String> getUploadS3Url(List<String> urls){
@@ -505,8 +498,7 @@ public class UploadToOssUtil {
 		return mfile;
 	}
 
-	private static FileItem createFileItem(String filePath)
-	{
+	private static FileItem createFileItem(String filePath) {
 		FileItemFactory factory = new DiskFileItemFactory(16, null);
 		String textFieldName = "textField";
 		int num = filePath.lastIndexOf(".");
@@ -756,6 +748,4 @@ public class UploadToOssUtil {
 		return null;
 	}
 
-
-
 }