阿里云oss分片上传,删除

转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/106843750 谢谢

pom

<!--        oss 阿里储存-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.8.1</version>
        </dependency>

yml

# OSS相关配置信息
aliyun:
  oss:
    endpoint: xxxx # oss对外服务的访问域名
    accessKeyId: xxxx # 访问身份验证中用到用户标识
    accessKeySecret: xxxx # 用户用于加密签名字符串和oss用来验证签名字符串的密钥
    bucketName: xxxx# oss的存储空间
    baseFile: zzz/ #指定上传文件夹
    urlPrefix: http://oss的存储空间.oss对外服务的访问域名/

工具类

oss分片上传方式一

	/**
	 * 阿里OSS对象存储文件上传工具类
	 *
	 * @author LHL
	 */
	@Slf4j
	@Component
	public class FileUploadOssUtil {
	
		private static String endpoint;
		private static String accessKeyId;
		private static String accessKeySecret;
		private static String bucketName;
		private static String baseFile;
		private static String urlPrefix;
	
		@Value("${aliyun.oss.endpoint}")
		public void setEndpoint(String endpoint) {
			this.endpoint = endpoint;
		}
	
		@Value("${aliyun.oss.accessKeyId}")
		public void setAccessKeyId(String accessKeyId) {
			this.accessKeyId = accessKeyId;
		}
	
		@Value("${aliyun.oss.accessKeySecret}")
		public void setAccessKeySecret(String accessKeySecret) {
			this.accessKeySecret = accessKeySecret;
		}
	
		@Value("${aliyun.oss.bucketName}")
		public void setBucketName(String bucketName) {
			this.bucketName = bucketName;
		}
	
		@Value("${aliyun.oss.baseFile}")
		public void setBaseFile(String baseFile) {
			this.baseFile = baseFile;
		}
	
		@Value("${aliyun.oss.urlPrefix}")
		public void setUrlPrefix(String urlPrefix) {
			this.urlPrefix = urlPrefix;
		}
	
		private static String key = null;
	
		private static OSS client = null;
	
		/**
		 * 定长线程池
		 */
		private static ExecutorService threadPool;
	
	
		private static List<PartETag> partETags = null;
	
		/**
		 * 2MB 分片大小
		 */
		private static final int partSize = 2 * 1024 * 1024;
	
		private static int counter = 0;
	
		/**
		 * 删除文件
		 */
		public static boolean remove(String filePath) {
			String path = filePath.replaceAll(urlPrefix, "");
			try {
				client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
				client.deleteObject(bucketName, path);
				log.info("oss删除成功 路径: {}", filePath);
			} catch (OSSException e) {
				log.error(e.getMessage(), e);
				return false;
			} finally {
				if (client != null) {
					client.shutdown();
				}
			}
			return true;
		}
		
		/*
		* 上传oss
		* @author LHL
		*/
		private static String upload(MultipartFile multipartFile, String baseDir) throws IOException {
			String fileName = multipartFile.getOriginalFilename();
			long start = System.currentTimeMillis();
			String[] names = fileName.split("\\.");
			String fileTypeName = names[names.length - 1];
			key = baseDir + "/" + UUID.randomUUID().toString().replaceAll("-", "") + "." + fileTypeName;
			client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
			String fileUrl = null;
			try {
				String uploadId = claimUploadId(key);
				log.info("申请一个新的上传id:" + uploadId);
				// 文件大小
				long fileLength = multipartFile.getSize();
				// 分片总数(总共分几个部分)
				int partCount = (int) (fileLength / partSize);
				if (fileLength % partSize != 0) {
					partCount++;
				}
				if (partCount > 10000) {
					log.warn("partCount总数不应超过10000");
					return null;
				} else {
					log.info("文件总共分片数:" + partCount);
				}
				partETags = Collections.synchronizedList(new ArrayList<PartETag>(partCount));
				CountDownLatch latch = new CountDownLatch(partCount);
				log.info("***************开始准备上传************");
				threadPool = ThreadConstants.getMyThreadPool();
				for (int i = 0; i < partCount; i++) {
					long startPos = i * partSize;
					long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
					threadPool.execute(new PartUploader(multipartFile, startPos, curPartSize, i + 1, uploadId, latch));
				}
				latch.await();
				if (partETags.size() != partCount) {
					StringBuilder partETagsStr = new StringBuilder("(");
					for (PartETag item : partETags) {
						partETagsStr.append(item.getPartNumber()).append(",");
					}
					partETagsStr.append(")");
					log.info(String.format("partCount:%s*******,partEtages:%s*******,partETagsSize:%s", partCount, partETagsStr, partETags.size()));
					throw new IllegalStateException("上传多个部分失败,因为有些部分还没有完成");
				} else {
					log.info("成功地将多个部分合并上传到一个名为的对象中 " + key);
				}
				listAllParts(uploadId);
				completeMultipartUpload(uploadId);
				log.info("获取一个对象");
				long end = System.currentTimeMillis();
				// 生成文件地址
				boolean isFileExist = client.doesObjectExist(bucketName, key);
				if (isFileExist) {
					fileUrl = urlPrefix + key;
					log.info(String.format("上传成功*****耗时:%s*****,文件地址:%s", ((end - start) / 1000), fileUrl));
				} else {
					throw new Exception("上传失败,文件不存在");
				}
			} catch (OSSException oe) {
				log.error(oe.getMessage(), oe);
			} catch (ClientException ce) {
				log.error(ce.getErrorMessage(), ce);
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				if (client != null) {
					client.shutdown();
				}
				partETags.clear();
				partETags = null;
	
			}
			return fileUrl;
		}
	
	
		/**
		 * 静态内部类,上传组件
		 */
		private static class PartUploader implements Runnable {
			private MultipartFile localFile;
			private long partSize;
			private int partNumber;
			private String uploadId;
			private long startPos;
			private CountDownLatch latch;
	
			public PartUploader(MultipartFile localFile, long startPos, long partSize, int partNumber, String uploadId, CountDownLatch latch) {
				this.localFile = localFile;
				this.partSize = partSize;
				this.partNumber = partNumber;
				this.uploadId = uploadId;
				this.startPos = startPos;
				this.latch = latch;
			}
	
			@Override
			public void run() {
				InputStream instream = null;
				try {
					log.info("Part#" + this.partNumber + " 开始上传\n");
					instream = localFile.getInputStream();
					instream.skip(startPos);
					UploadPartRequest uploadPartRequest = new UploadPartRequest();
					uploadPartRequest.setBucketName(bucketName);
					uploadPartRequest.setKey(key);
					uploadPartRequest.setUploadId(this.uploadId);
					uploadPartRequest.setInputStream(instream);
					uploadPartRequest.setPartSize(this.partSize);
					uploadPartRequest.setPartNumber(this.partNumber);
					UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
					log.info("Part#" + this.partNumber + " 完毕\n");
					synchronized (partETags) {
						partETags.add(uploadPartResult.getPartETag());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (instream != null) {
						try {
							instream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					latch.countDown();
				}
			}
		}
	
		private static String claimUploadId(String path) {
			InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
			// 大家可根据自己的文件类型,设置不同的响应content-type
			String contentType = getContentType(path);
			if (StringUtils.isNotBlank(contentType)) {
				request.addHeader("Content-Type", contentType);
			}
			request.addHeader("Cache-Control", "no-cache");
			InitiateMultipartUploadResult result = client.initiateMultipartUpload(request);
			return result.getUploadId();
		}
	
	     //有些不常用的可能有问题 
		private static String getContentType(String inputPath) {
			String type = inputPath.substring(inputPath.lastIndexOf(".") + 1).toLowerCase();
			// 判断是否为音频 mp3、aac、ape/flac、wav、wma、amr、mid
			if (EnglishConstant.MP3.equalsIgnoreCase(type)) {
				return "audio/mpeg";
			} else if (EnglishConstant.AAC.equalsIgnoreCase(type)) {
				return "audio/x-mei-aac";
			} else if (EnglishConstant.APE.equalsIgnoreCase(type)) {
				return "audio/mpeg";
			} else if (EnglishConstant.WAV.equalsIgnoreCase(type)) {
				return "video/x-ms-wmv";
			} else if (EnglishConstant.WMA.equalsIgnoreCase(type)) {
				return "audio/x-ms-wma";
			} else if (EnglishConstant.AMR.equalsIgnoreCase(type)) {
				return "audio/AMR";
			} else if (EnglishConstant.MID.equalsIgnoreCase(type)) {
				return "audio/mid";
			}
			//判断是否为图片 jpg、png、gif、bmp、ico、tif(tiff)、psd/psb、WebP、RAW、pdf、DCM、sai/rif
			else if (EnglishConstant.JPG.equalsIgnoreCase(type)) {
				return "image/jpg";
			} else if (EnglishConstant.JPEG.equalsIgnoreCase(type)) {
				return "image/jpg";
			} else if (EnglishConstant.PNG.equalsIgnoreCase(type)) {
				return "image/jpg";
			} else if (EnglishConstant.GIF.equalsIgnoreCase(type)) {
				return "image/gif";
			} else if (EnglishConstant.TIF.equalsIgnoreCase(type)) {
				return "image/tiff";
			} else if (EnglishConstant.TIFF.equalsIgnoreCase(type)) {
				return "image/tiff";
			} else if (EnglishConstant.BMP.equalsIgnoreCase(type)) {
				return "image/bmp";
			} else if (EnglishConstant.PDF.equalsIgnoreCase(type)) {
				return "application/pdf";
			} else if (EnglishConstant.PSD.equalsIgnoreCase(type)) {
				return "application/vnd.ms-xpsdocument";
			} else if (EnglishConstant.PSB.equalsIgnoreCase(type)) {
				return "image/jpeg";
			}
			//判断是否为视频 mp4/m4v/3gp/mpg、flv/f4v/swf、avi、wmv、rmvb、mov、mts/m2t、webm/ogg/mkv
			else if (EnglishConstant.MP4.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.AVI.equalsIgnoreCase(type)) {
				return "video/avi";
			} else if (EnglishConstant.MOV.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.FLV.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.WMV.equalsIgnoreCase(type)) {
				return "video/x-ms-wmv";
			} else if (EnglishConstant.ASF.equalsIgnoreCase(type)) {
				return "allpication/vnd.ms-asf";
			} else if (EnglishConstant.ASX.equalsIgnoreCase(type)) {
				return "allpication/vnd.ms-asf";
			} else if (EnglishConstant.WMV9.equalsIgnoreCase(type)) {
				return "video/x-ms-wmv";
			} else if (EnglishConstant.GP3.equalsIgnoreCase(type)) {
				return "audio/3gpp";
			} else if (EnglishConstant.MPG.equalsIgnoreCase(type)) {
				return "video/mpeg";
			} else if (EnglishConstant.RM.equalsIgnoreCase(type)) {
				return "application/vnd.rn-realmedia";
			} else if (EnglishConstant.RM_VB.equalsIgnoreCase(type)) {
				return "application/x-shockwave-flash";
			} else if (EnglishConstant.MTS.equalsIgnoreCase(type)) {
				return "model/vnd.mts";
			} else if (EnglishConstant.M2T.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.OGG.equalsIgnoreCase(type)) {
				return "application/ogg";
			} else if (EnglishConstant.M4V.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.MKV.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.F4V.equalsIgnoreCase(type)) {
				return "video/mp4";
			} else if (EnglishConstant.SWF.equalsIgnoreCase(type)) {
				return "application/x-shockwave-flash";
			} else if (EnglishConstant.WEBM.equalsIgnoreCase(type)) {
				return "video/mp4";
			}
			//apk
			else if (EnglishConstant.APK.equalsIgnoreCase(type)){
				return 	"application/vnd.android.package-archive";
			}
			return null;
		}

	
	
		private static void completeMultipartUpload(String uploadId) {
			// Make part numbers in ascending order
			Collections.sort(partETags, new Comparator<PartETag>() {
	
				@Override
				public int compare(PartETag p1, PartETag p2) {
					return p1.getPartNumber() - p2.getPartNumber();
				}
			});
	
			log.info("Completing to upload multiparts\n");
			CompleteMultipartUploadRequest completeMultipartUploadRequest =
				new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
			client.completeMultipartUpload(completeMultipartUploadRequest);
		}
	
		private static void listAllParts(String uploadId) {
			log.info("Listing all parts......");
			ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
			PartListing partListing = client.listParts(listPartsRequest);
	
			int partCount = partListing.getParts().size();
			for (int i = 0; i < partCount; i++) {
				PartSummary partSummary = partListing.getParts().get(i);
				log.info("\tPart#" + partSummary.getPartNumber() + ", ETag=" + partSummary.getETag());
			}
		}
	
	
		/**
		 * 上传执行器
		 */
		public synchronized static String fileUpload(MultipartFile multipartFile) {
			ReentrantLock lock = new ReentrantLock();
			lock.lock();
			long start = System.currentTimeMillis();
			String fileName = multipartFile.getOriginalFilename();
			String baseDir = checkContentType(fileName);
			try {
				baseDir = baseFile + baseDir + "/" + DateUtil.parseDateToStr("yyyyMM", new Date());
				String fileUrl = upload(multipartFile, baseDir);
				long end = System.currentTimeMillis();
				log.info("文件上传结束,共耗时" + (end - start) + "ms");
				return fileUrl;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} finally {
				lock.unlock();
			}
		}
	
		private static final File getAbsoluteFile(String filename) throws IOException {
			File desc = new File(File.separator + filename);
	
			if (!desc.getParentFile().exists()) {
				desc.getParentFile().mkdirs();
			}
			if (!desc.exists()) {
				desc.createNewFile();
			}
			return desc;
		}
	
		private static final String encodingFilename(String filename, String extension) {
			filename = filename.replace("_", " ");
			filename = MD5Util.md5Encrypt32Lower(filename + System.nanoTime() + counter++) + extension;
			return filename;
		}
	
	
		public static String checkContentType(String inputPath) {
			String type = inputPath.substring(inputPath.lastIndexOf(".") + 1).toLowerCase();
			// 判断是否为音频 mp3、aac、ape/flac、wav、wma、amr、mid
			if (EnglishConstant.MP3.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.AAC.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.APE.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.WAV.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.WMA.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.AMR.equalsIgnoreCase(type)) {
				return "audio";
			} else if (EnglishConstant.MID.equalsIgnoreCase(type)) {
				return "audio";
			}
			//判断是否为图片 jpg、png、gif、bmp、ico、tif(tiff)、psd/psb、WebP、RAW、pdf、DCM、sai/rif
			else if (EnglishConstant.JPG.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.JPEG.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.PNG.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.GIF.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.TIF.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.TIFF.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.BMP.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.PDF.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.PSD.equalsIgnoreCase(type)) {
				return "picture";
			} else if (EnglishConstant.PSB.equalsIgnoreCase(type)) {
				return "picture";
			}
			//判断是否为视频 mp4/m4v/3gp/mpg、flv/f4v/swf、avi、wmv、rmvb、mov、mts/m2t、webm/ogg/mkv
			else if (EnglishConstant.MP4.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.AVI.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.MOV.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.FLV.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.WMV.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.ASF.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.ASX.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.WMV9.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.GP3.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.MPG.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.RM.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.RM_VB.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.MTS.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.M2T.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.OGG.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.M4V.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.MKV.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.F4V.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.SWF.equalsIgnoreCase(type)) {
				return "video";
			} else if (EnglishConstant.WEBM.equalsIgnoreCase(type)) {
				return "video";
			}
			//apk
			else if (EnglishConstant.APK.equalsIgnoreCase(type)){
				return "apk";
			}
			return "other";
		}
	}
	

oss分片上传方式二

		/**
		 * @ClassName: OssConfig
		 * @Auther: LHL
		 * @Description:
		 * @Version 1.0
		 */
		@Component
		@Slf4j
		public class OssConfig {
		
			@Value("${aliyun.oss.endpoint}")
			private String endpoint;
		
			@Value("${aliyun.oss.accessKeyId}")
			private String accessKeyId;
		
			@Value("${aliyun.oss.accessKeySecret}")
			private String accessKeySecret;
		
			private BlockingQueue<OSS> ossBlockingQueue = new LinkedBlockingDeque<>();
		
			/**
			 * @Description: 获取client
			 * @method: getOssClient
			 * @Param:
			 * @return: com.aliyun.oss.OSS
			 * @auther: LHL
			 */
			public OSS getOssClient() {
				try {
					OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
					log.info("oss client 连接");
					ossBlockingQueue.put(client);
					log.info("oss client put Queue");
				} catch (InterruptedException e) {
					log.error("oss client put Queue fail");
					e.printStackTrace();
				}
				OSS take = null;
				try {
					take = ossBlockingQueue.take();
					log.info("oss client take Queue");
				} catch (InterruptedException e) {
					log.error("oss client take Queue fail");
					e.printStackTrace();
				}
				return take;
			}
		
			/**
			 * @Description: 关闭oss client
			 * @method: shutdown
			 * @Param:
			 * @return: void
			 * @auther: LHL
			 */
			public void shutdown(OSS client) {
				if (client != null) {
					client.shutdown();
					log.info("关闭 oss client");
				}
			}
		}
		/**
		 * 阿里OSS对象存储文件上传工具类
		 *
		 * @author LHL
		 * @since 2020-06-15
		 */
		@Slf4j
		@Component
		public class FileUploadOssUtil {
		
			@Value("${aliyun.oss.bucketName}")
			private String bucketName;
		
			@Value("${aliyun.oss.baseFile}")
			private String baseFile;
		
			@Value("${aliyun.oss.urlPrefix}")
			private String urlPrefix;
		
			@Autowired
			private OssConfig ossConfig;
		
			private String key = null;
		
			/**
			 * 定长线程池
			 */
			private ExecutorService threadPool;
		
		
			private List<PartETag> partETags = null;
		
			/**
			 * 2MB 分片大小
			 */
			private final int partSize = 2 * 1024 * 1024;
		
			private int counter = 0;
		
			/**
			 * 删除文件
			 */
			public boolean remove(String filePath) {
				String path = filePath.replaceAll(urlPrefix, "");
				OSS client = null;
				try {
					client = ossConfig.getOssClient();
					client.deleteObject(bucketName, path);
					log.info("oss删除成功 路径: {}", filePath);
				} catch (OSSException e) {
					log.error("OSSException: {}", e.getMessage(), e);
					return false;
				} finally {
					ossConfig.shutdown(client);
				}
				return true;
			}
		
			private String upload(MultipartFile multipartFile, String baseDir) {
				OSS client = null;
				String fileName = multipartFile.getOriginalFilename();
				long start = System.currentTimeMillis();
				String[] names = fileName.split("\\.");
				String fileTypeName = names[names.length - 1];
				key = baseDir + "/" + UUID.randomUUID().toString().replaceAll("-", "") + "." + fileTypeName;
				String fileUrl = null;
				try {
					client = ossConfig.getOssClient();
					String uploadId = claimUploadId(key, client);
					log.info("申请一个新的上传id:" + uploadId);
					// 文件大小
					long fileLength = multipartFile.getSize();
					// 分片总数(总共分几个部分)
					int partCount = (int) (fileLength / partSize);
					if (fileLength % partSize != 0) {
						partCount++;
					}
					if (partCount > 10000) {
						log.warn("partCount总数不应超过10000");
						return null;
					} else {
						log.info("文件总共分片数:" + partCount);
					}
					partETags = Collections.synchronizedList(new ArrayList<PartETag>(partCount));
					CountDownLatch latch = new CountDownLatch(partCount);
					log.info("***************开始准备上传************");
					threadPool = ThreadConstants.getMyThreadPool();
					for (int i = 0; i < partCount; i++) {
						long startPos = i * partSize;
						long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
						threadPool.execute(new PartUploader(multipartFile, startPos, curPartSize, i + 1, uploadId, latch, client));
					}
					latch.await();
					if (partETags.size() != partCount) {
						StringBuilder partETagsStr = new StringBuilder("(");
						for (PartETag item : partETags) {
							partETagsStr.append(item.getPartNumber()).append(",");
						}
						partETagsStr.append(")");
						log.info(String.format("partCount:%s*******,partEtages:%s*******,partETagsSize:%s", partCount, partETagsStr, partETags.size()));
						throw new IllegalStateException("上传多个部分失败,因为有些部分还没有完成");
					} else {
						log.info("成功地将多个部分合并上传到一个名为的对象中 " + key);
					}
					listAllParts(uploadId, client);
					completeMultipartUpload(uploadId, client);
					log.info("获取一个对象");
					long end = System.currentTimeMillis();
					// 生成文件地址
					boolean isFileExist = client.doesObjectExist(bucketName, key);
					if (isFileExist) {
						fileUrl = urlPrefix + key;
						log.info(String.format("上传成功*****耗时:%s*****,文件地址:%s", ((end - start) / 1000), fileUrl));
					} else {
						throw new Exception("上传失败,文件不存在");
					}
				} catch (OSSException oe) {
					log.error(oe.getMessage(), oe);
				} catch (ClientException ce) {
					log.error(ce.getErrorMessage(), ce);
				} catch (Exception e) {
					log.error(e.getMessage(), e);
				} finally {
					ossConfig.shutdown(client);
					partETags.clear();
					partETags = null;
				}
				return fileUrl;
			}
		
		
			/**
			 * 静态内部类,上传组件
			 */
			private class PartUploader implements Runnable {
				private MultipartFile localFile;
				private long partSize;
				private int partNumber;
				private String uploadId;
				private long startPos;
				private CountDownLatch latch;
				private OSS client;
		
				public PartUploader(MultipartFile localFile, long startPos, long partSize, int partNumber, String uploadId, CountDownLatch latch, OSS client) {
					this.localFile = localFile;
					this.partSize = partSize;
					this.partNumber = partNumber;
					this.uploadId = uploadId;
					this.startPos = startPos;
					this.latch = latch;
					this.client = client;
				}
		
				@Override
				public void run() {
					InputStream instream = null;
					try {
						log.info("Part#" + this.partNumber + " 开始上传\n");
						instream = localFile.getInputStream();
						instream.skip(startPos);
						UploadPartRequest uploadPartRequest = new UploadPartRequest();
						uploadPartRequest.setBucketName(bucketName);
						uploadPartRequest.setKey(key);
						uploadPartRequest.setUploadId(this.uploadId);
						uploadPartRequest.setInputStream(instream);
						uploadPartRequest.setPartSize(this.partSize);
						uploadPartRequest.setPartNumber(this.partNumber);
						UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
						log.info("Part#" + this.partNumber + " 完毕\n");
						synchronized (partETags) {
							partETags.add(uploadPartResult.getPartETag());
						}
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						if (instream != null) {
							try {
								instream.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						latch.countDown();
					}
				}
			}
		
			private String claimUploadId(String path, OSS client) {
				InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
				// 大家可根据自己的文件类型,设置不同的响应content-type
				String contentType = getContentType(path);
				if (StringUtils.isNotBlank(contentType)) {
					request.addHeader("Content-Type", contentType);
				}
				request.addHeader("Cache-Control", "no-cache");
				InitiateMultipartUploadResult result = client.initiateMultipartUpload(request);
				return result.getUploadId();
			}
		
			private static String getContentType(String inputPath) {
				String type = inputPath.substring(inputPath.lastIndexOf(".") + 1).toLowerCase();
				// 判断是否为音频 mp3、aac、ape/flac、wav、wma、amr、mid
				if (EnglishConstant.MP3.equalsIgnoreCase(type)) {
					return "audio/mpeg";
				} else if (EnglishConstant.AAC.equalsIgnoreCase(type)) {
					return "audio/x-mei-aac";
				} else if (EnglishConstant.APE.equalsIgnoreCase(type)) {
					return "audio/mpeg";
				} else if (EnglishConstant.WAV.equalsIgnoreCase(type)) {
					return "video/x-ms-wmv";
				} else if (EnglishConstant.WMA.equalsIgnoreCase(type)) {
					return "audio/x-ms-wma";
				} else if (EnglishConstant.AMR.equalsIgnoreCase(type)) {
					return "audio/AMR";
				} else if (EnglishConstant.MID.equalsIgnoreCase(type)) {
					return "audio/mid";
				}
				//判断是否为图片 jpg、png、gif、bmp、ico、tif(tiff)、psd/psb、WebP、RAW、pdf、DCM、sai/rif
				else if (EnglishConstant.JPG.equalsIgnoreCase(type)) {
					return "image/jpg";
				} else if (EnglishConstant.JPEG.equalsIgnoreCase(type)) {
					return "image/jpg";
				} else if (EnglishConstant.PNG.equalsIgnoreCase(type)) {
					return "image/jpg";
				} else if (EnglishConstant.GIF.equalsIgnoreCase(type)) {
					return "image/gif";
				} else if (EnglishConstant.TIF.equalsIgnoreCase(type)) {
					return "image/tiff";
				} else if (EnglishConstant.TIFF.equalsIgnoreCase(type)) {
					return "image/tiff";
				} else if (EnglishConstant.BMP.equalsIgnoreCase(type)) {
					return "image/bmp";
				} else if (EnglishConstant.PDF.equalsIgnoreCase(type)) {
					return "application/pdf";
				} else if (EnglishConstant.PSD.equalsIgnoreCase(type)) {
					return "application/vnd.ms-xpsdocument";
				} else if (EnglishConstant.PSB.equalsIgnoreCase(type)) {
					return "image/jpeg";
				}
				//判断是否为视频 mp4/m4v/3gp/mpg、flv/f4v/swf、avi、wmv、rmvb、mov、mts/m2t、webm/ogg/mkv
				else if (EnglishConstant.MP4.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.AVI.equalsIgnoreCase(type)) {
					return "video/avi";
				} else if (EnglishConstant.MOV.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.FLV.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.WMV.equalsIgnoreCase(type)) {
					return "video/x-ms-wmv";
				} else if (EnglishConstant.ASF.equalsIgnoreCase(type)) {
					return "allpication/vnd.ms-asf";
				} else if (EnglishConstant.ASX.equalsIgnoreCase(type)) {
					return "allpication/vnd.ms-asf";
				} else if (EnglishConstant.WMV9.equalsIgnoreCase(type)) {
					return "video/x-ms-wmv";
				} else if (EnglishConstant.GP3.equalsIgnoreCase(type)) {
					return "audio/3gpp";
				} else if (EnglishConstant.MPG.equalsIgnoreCase(type)) {
					return "video/mpeg";
				} else if (EnglishConstant.RM.equalsIgnoreCase(type)) {
					return "application/vnd.rn-realmedia";
				} else if (EnglishConstant.RM_VB.equalsIgnoreCase(type)) {
					return "application/x-shockwave-flash";
				} else if (EnglishConstant.MTS.equalsIgnoreCase(type)) {
					return "model/vnd.mts";
				} else if (EnglishConstant.M2T.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.OGG.equalsIgnoreCase(type)) {
					return "application/ogg";
				} else if (EnglishConstant.M4V.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.MKV.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.F4V.equalsIgnoreCase(type)) {
					return "video/mp4";
				} else if (EnglishConstant.SWF.equalsIgnoreCase(type)) {
					return "application/x-shockwave-flash";
				} else if (EnglishConstant.WEBM.equalsIgnoreCase(type)) {
					return "video/mp4";
				}
				//apk
				else if (EnglishConstant.APK.equalsIgnoreCase(type)){
					return 	"application/vnd.android.package-archive";
				}
				return null;
			}

		
		
			private void completeMultipartUpload(String uploadId, OSS client) {
				// Make part numbers in ascending order
				Collections.sort(partETags, new Comparator<PartETag>() {
					@Override
					public int compare(PartETag p1, PartETag p2) {
						return p1.getPartNumber() - p2.getPartNumber();
					}
				});
		
				log.info("Completing to upload multiparts\n");
				CompleteMultipartUploadRequest completeMultipartUploadRequest =
					new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
				client.completeMultipartUpload(completeMultipartUploadRequest);
			}
		
			private void listAllParts(String uploadId, OSS client) {
				log.info("Listing all parts......");
				ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
				PartListing partListing = client.listParts(listPartsRequest);
		
				int partCount = partListing.getParts().size();
				for (int i = 0; i < partCount; i++) {
					PartSummary partSummary = partListing.getParts().get(i);
					log.info("\tPart#" + partSummary.getPartNumber() + ", ETag=" + partSummary.getETag());
				}
			}
		
			/**
			 * 上传执行器
			 */
			public synchronized String fileUpload(MultipartFile multipartFile, String baseDir) {
				ReentrantLock lock = new ReentrantLock();
				lock.lock();
				try {
					long start = System.currentTimeMillis();
					baseDir = baseFile + baseDir + "/" + DateUtil.parseDateToStr("yyyyMM", new Date());
					String fileUrl = upload(multipartFile, baseDir);
					long end = System.currentTimeMillis();
					log.info("文件上传结束,共耗时" + (end - start) + "ms");
					return fileUrl;
				} finally {
					lock.unlock();
				}
			}
		
		
			/**
			 * 上传执行器
			 */
			public synchronized String fileUpload(MultipartFile multipartFile) {
				ReentrantLock lock = new ReentrantLock();
				lock.lock();
				try {
					long start = System.currentTimeMillis();
					String fileName = multipartFile.getOriginalFilename();
					String baseDir = checkContentType(fileName);
					baseDir = baseFile + baseDir + "/" + DateUtil.parseDateToStr("yyyyMM", new Date());
					String fileUrl = upload(multipartFile, baseDir);
					long end = System.currentTimeMillis();
					log.info("文件上传结束,共耗时" + (end - start) + "ms");
					return fileUrl;
				} finally {
					lock.unlock();
				}
			}
		
			private File getAbsoluteFile(String filename) throws IOException {
				File desc = new File(File.separator + filename);
		
				if (!desc.getParentFile().exists()) {
					desc.getParentFile().mkdirs();
				}
				if (!desc.exists()) {
					desc.createNewFile();
				}
				return desc;
			}
		
			private String encodingFilename(String filename, String extension) {
				filename = filename.replace("_", " ");
				filename = MD5Util.md5Encrypt32Lower(filename + System.nanoTime() + counter++) + extension;
				return filename;
			}
		
		
			private String getBaseDir(String inputPath) {
				String type = inputPath.substring(inputPath.lastIndexOf(".") + 1).toLowerCase();
				// 判断是否为音频 mp3、aac、ape/flac、wav、wma、amr、mid
				if (EnglishConstant.MP3.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.AAC.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.APE.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.WAV.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.WMA.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.AMR.equalsIgnoreCase(type)) {
					return "audio";
				} else if (EnglishConstant.MID.equalsIgnoreCase(type)) {
					return "audio";
				}
				//判断是否为图片 jpg、png、gif、bmp、ico、tif(tiff)、psd/psb、WebP、RAW、pdf、DCM、sai/rif
				else if (EnglishConstant.JPG.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.JPEG.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.PNG.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.GIF.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.TIF.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.TIFF.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.BMP.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.PDF.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.PSD.equalsIgnoreCase(type)) {
					return "picture";
				} else if (EnglishConstant.PSB.equalsIgnoreCase(type)) {
					return "picture";
				}
				//判断是否为视频 mp4/m4v/3gp/mpg、flv/f4v/swf、avi、wmv、rmvb、mov、mts/m2t、webm/ogg/mkv
				else if (EnglishConstant.MP4.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.AVI.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.MOV.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.FLV.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.WMV.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.ASF.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.ASX.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.WMV9.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.GP3.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.MPG.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.RM.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.RM_VB.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.MTS.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.M2T.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.OGG.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.M4V.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.MKV.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.F4V.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.SWF.equalsIgnoreCase(type)) {
					return "video";
				} else if (EnglishConstant.WEBM.equalsIgnoreCase(type)) {
					return "video";
				}
				//apk
				else if (EnglishConstant.APK.equalsIgnoreCase(type)){
					return "apk";
				}
				return "other";
			}
		}

其他相关类


/**
 * 获取静态单例固定线程池
 *
 * @author LHL
 */
public class ThreadConstants {


	public static final int MAX_THREAD_COUNT = 10;

	/**
	 * 自定义线程池
	 */
	private static ExecutorService MY_THREAD_POOL;

	/**
	 * 自定义线程池
	 *
	 * @author LHL
	 */
	public static ExecutorService getMyThreadPool() {
		if (MY_THREAD_POOL == null) {
			ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("upload-pool-%d").build();
			 MY_THREAD_POOL = new ThreadPoolExecutor(MAX_THREAD_COUNT, MAX_THREAD_COUNT,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
		}
		return MY_THREAD_POOL;
	}
}

public class EnglishConstant {
	String SPOT = ".";

	String MP3 = "mp3";

	String AAC = "aac";

	String APE = "ape";

	String WAV = "wav";

	String WMA = "wma";

	String AMR = "amr";

	String MID = "mid";

	String JPG = "jpg";

	String JPEG = "jpeg";

	String PNG = "png";

	String GIF = "gif";

	String TIF = "tif";

	String TIFF = "tiff";

	String BMP = "bmp";

	String PDF = "pdf";

	String PSD = "psd";

	String PSB = "psb";

	String MP4 = "mp4";

	String AVI = "avi";

	String MOV = "mov";

	String FLV = "flv";

	String WMV = "wmv";

	String ASF = "asf";

	String ASX = "asx";

	String WMV9 = "wmv9";

	String GP3 = "3gp";

	String MPG = "mpg";

	String RM = "rm";

	String RM_VB = "rmvb";

	String MTS = "mts";

	String M2T = "m2t";

	String OGG = "ogg";

	String M4V = "m4v";

	String MKV = "mkv";

	String F4V = "f4v";

	String SWF = "swf";

	String WEBM = "webm";

	String APK = "apk";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值