OssCheckPointUploadUtil.java 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.fdkanfang.common.util;
  2. import com.aliyun.oss.*;
  3. import com.aliyun.oss.model.CompleteMultipartUploadResult;
  4. import com.aliyun.oss.model.UploadFileRequest;
  5. import com.aliyun.oss.model.UploadFileResult;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Service;
  9. import java.io.File;
  10. /**
  11. * @author abnerhou
  12. * @date 2020/5/28 10:37
  13. * @desciption
  14. */
  15. @Service
  16. @Slf4j
  17. public class OssCheckPointUploadUtil {
  18. @Value("${oss.point}")
  19. private String endpoint;
  20. @Value("${oss.key}")
  21. private String accessKeyId;
  22. @Value("${oss.secrey}")
  23. private String accessKeySecret;
  24. @Value("${oss.bucket}")
  25. private String bucketName;
  26. public void doUploadThenDelete(String uploadFile , String objectName){
  27. // 创建OSSClient实例。
  28. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  29. try {
  30. File localFile = new File(uploadFile);
  31. log.info("Object name = {}" ,objectName);
  32. UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, objectName);
  33. // The local file to upload---it must exist.
  34. // 指定上传的本地文件。
  35. uploadFileRequest.setUploadFile(uploadFile);
  36. log.info("本地文件为:{}" ,uploadFile);
  37. // Sets the concurrent upload task number to 5.
  38. uploadFileRequest.setTaskNum(5);
  39. // Sets the part size to 1MB.
  40. uploadFileRequest.setPartSize(1024 * 1024 * 1);
  41. // 开启断点续传,默认关闭。
  42. // Enables the checkpoint file. By default it's off.
  43. uploadFileRequest.setEnableCheckpoint(true);
  44. // 记录本地分片上传结果的文件。开启断点续传功能时需要设置此参数,上传过程中的进度信息会保存在该文件中,如果某一分片上传失败,再次上传时会根据文件中记录的点继续上传。上传完成后,该文件会被删除。默认与待上传的本地文件同目录,为uploadFile.ucp。
  45. String localCheckFile = localFile.getName() + "_uploadFile.ucp";
  46. uploadFileRequest.setCheckpointFile(localCheckFile);
  47. UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
  48. CompleteMultipartUploadResult multipartUploadResult =
  49. uploadResult.getMultipartUploadResult();
  50. log.info("断点续传结果:{}", multipartUploadResult.getETag());
  51. } catch (OSSException oe) {
  52. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  53. + "but was rejected with an error response for some reason.");
  54. System.out.println("Error Message: " + oe.getErrorMessage());
  55. System.out.println("Error Code: " + oe.getErrorCode());
  56. System.out.println("Request ID: " + oe.getRequestId());
  57. System.out.println("Host ID: " + oe.getHostId());
  58. } catch (ClientException ce) {
  59. System.out.println("Caught an ClientException, which means the client encountered "
  60. + "a serious internal problem while trying to communicate with OSS, "
  61. + "such as not being able to access the network.");
  62. System.out.println("Error Message: " + ce.getMessage());
  63. } catch (Throwable e) {
  64. e.printStackTrace();
  65. } finally {
  66. ossClient.shutdown();
  67. FileUtils.deleteFile(uploadFile);
  68. log.info("关闭oss client,并且本地删除文件");
  69. }
  70. }
  71. public void upload2(String filePath, String key1) {
  72. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  73. try {
  74. File file = new File(filePath);
  75. if (!file.exists()) {
  76. log.error("要上传的文件不存在:" + filePath);
  77. }
  78. ossClient.putObject(bucketName, key1, file);
  79. } catch (Exception e) {
  80. log.error(e.toString() + filePath);
  81. }
  82. }
  83. }