当前位置:   article > 正文

七牛云存储:断点续传_七牛云断点续传

七牛云断点续传
  1. package com.learn.qiniu;
  2. import com.google.gson.Gson;
  3. import com.qiniu.common.QiniuException;
  4. import com.qiniu.common.Zone;
  5. import com.qiniu.http.Response;
  6. import com.qiniu.storage.Configuration;
  7. import com.qiniu.storage.Region;
  8. import com.qiniu.storage.UploadManager;
  9. import com.qiniu.storage.model.DefaultPutRet;
  10. import com.qiniu.storage.persistent.FileRecorder;
  11. import com.qiniu.util.Auth;
  12. import org.junit.Test;
  13. import java.io.IOException;
  14. import java.nio.file.Paths;
  15. public class QiniuDemo {
  16. //断点续传
  17. @Test
  18. public void testUpload02() {
  19. //构造一个带指定Zone对象的配置类
  20. Configuration cfg = new Configuration(Region.region2());
  21. //...其他参数参考类注释
  22. //...生成上传凭证,然后准备上传
  23. String accessKey = "ojcdZhgxHynIK7Va9DH2OGc9YG94lTzyAsCJn_8e8";
  24. String secretKey = "AFW9QSV8BAZ8p1vh_dKDm30BBybvIWAjAhQWUyMZ8";
  25. String bucket = "learn-bucket";
  26. //如果是Windows情况下,格式是 D:\\qiniu\\test.png
  27. String localFilePath = "C:\\Users\\leon\\Desktop\\00\\test.xlsx";
  28. //默认不指定key的情况下,以文件内容的hash值作为文件名
  29. String key = "testExcel";
  30. Auth auth = Auth.create(accessKey, secretKey);
  31. String upToken = auth.uploadToken(bucket);
  32. //断点续传:
  33. String localTempDir = Paths.get(System.getProperty("java.io.tmpdir"), bucket).toString();
  34. System.out.println(localTempDir);
  35. try {
  36. //设置断点续传文件进度保存目录
  37. FileRecorder fileRecorder = new FileRecorder(localTempDir);
  38. UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
  39. try {
  40. Response response = uploadManager.put(localFilePath, key, upToken);
  41. //解析上传成功的结果
  42. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  43. System.out.println(putRet.key);
  44. System.out.println(putRet.hash);
  45. } catch (QiniuException ex) {
  46. Response r = ex.response;
  47. System.err.println(r.toString());
  48. try {
  49. System.err.println(r.bodyString());
  50. } catch (QiniuException ex2) {
  51. //ignore
  52. }
  53. }
  54. } catch (IOException ex) {
  55. ex.printStackTrace();
  56. }
  57. }
  58. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/604118
推荐阅读
相关标签
  

闽ICP备14008679号