1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package com.fdkanfang.common.util;
- import com.aliyun.oss.*;
- import com.aliyun.oss.model.CompleteMultipartUploadResult;
- import com.aliyun.oss.model.UploadFileRequest;
- import com.aliyun.oss.model.UploadFileResult;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import java.io.File;
- /**
- * @author abnerhou
- * @date 2020/5/28 10:37
- * @desciption
- */
- @Service
- @Slf4j
- public class OssCheckPointUploadUtil {
- @Value("${oss.point}")
- private String endpoint;
- @Value("${oss.key}")
- private String accessKeyId;
- @Value("${oss.secrey}")
- private String accessKeySecret;
- @Value("${oss.bucket}")
- 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);
- UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, objectName);
- // The local file to upload---it must exist.
- // 指定上传的本地文件。
- uploadFileRequest.setUploadFile(uploadFile);
- log.info("本地文件为:{}" ,uploadFile);
- // Sets the concurrent upload task number to 5.
- uploadFileRequest.setTaskNum(5);
- // Sets the part size to 1MB.
- uploadFileRequest.setPartSize(1024 * 1024 * 1);
- // 开启断点续传,默认关闭。
- // Enables the checkpoint file. By default it's off.
- uploadFileRequest.setEnableCheckpoint(true);
- // 记录本地分片上传结果的文件。开启断点续传功能时需要设置此参数,上传过程中的进度信息会保存在该文件中,如果某一分片上传失败,再次上传时会根据文件中记录的点继续上传。上传完成后,该文件会被删除。默认与待上传的本地文件同目录,为uploadFile.ucp。
- String localCheckFile = localFile.getName() + "_uploadFile.ucp";
- uploadFileRequest.setCheckpointFile(localCheckFile);
- UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
- CompleteMultipartUploadResult multipartUploadResult =
- uploadResult.getMultipartUploadResult();
- log.info("断点续传结果:{}", multipartUploadResult.getETag());
- } catch (OSSException oe) {
- System.out.println("Caught an OSSException, which means your request made it to OSS, "
- + "but was rejected with an error response for some reason.");
- System.out.println("Error Message: " + oe.getErrorMessage());
- System.out.println("Error Code: " + oe.getErrorCode());
- System.out.println("Request ID: " + oe.getRequestId());
- System.out.println("Host ID: " + oe.getHostId());
- } catch (ClientException ce) {
- System.out.println("Caught an ClientException, which means the client encountered "
- + "a serious internal problem while trying to communicate with OSS, "
- + "such as not being able to access the network.");
- System.out.println("Error Message: " + ce.getMessage());
- } catch (Throwable e) {
- e.printStackTrace();
- } finally {
- ossClient.shutdown();
- FileUtils.deleteFile(uploadFile);
- log.info("关闭oss client,并且本地删除文件");
- }
- }
- public void upload2(String filePath, String key1) {
- OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- log.error("要上传的文件不存在:" + filePath);
- }
- ossClient.putObject(bucketName, key1, file);
- } catch (Exception e) {
- log.error(e.toString() + filePath);
- }
- }
- }
|