QiniuUpload.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.fdkankan.fyun.qiniu;
  2. import com.google.gson.Gson;
  3. import com.qiniu.cdn.CdnManager;
  4. import com.qiniu.cdn.CdnResult;
  5. import com.qiniu.common.QiniuException;
  6. import com.qiniu.http.Response;
  7. import com.qiniu.storage.BucketManager;
  8. import com.qiniu.storage.Configuration;
  9. import com.qiniu.storage.UploadManager;
  10. import com.qiniu.storage.model.DefaultPutRet;
  11. import com.qiniu.util.Auth;
  12. import com.qiniu.util.StringMap;
  13. import com.qiniu.util.UrlSafeBase64;
  14. import java.io.File;
  15. public class QiniuUpload {
  16. //删除文件
  17. public static void delete(String key) throws QiniuException{
  18. Configuration cfg = new Configuration(QiniuUtil.zone);
  19. Auth auth = Auth.create(QiniuUtil.accessKey, QiniuUtil.secretKey);
  20. BucketManager bucketManager = new BucketManager(auth, cfg);
  21. bucketManager.delete(QiniuUtil.bucket, key);
  22. }
  23. //刷新文件
  24. public static void refresh(String url) throws QiniuException{
  25. String [] urls = {url};
  26. Auth auth = Auth.create(QiniuUtil.accessKey, QiniuUtil.secretKey);
  27. CdnManager c = new CdnManager(auth);
  28. CdnResult.RefreshResult response = c.refreshUrls(urls);
  29. }
  30. /**
  31. * 上传单个文件到七牛云
  32. * @param key
  33. * @param localFilePath
  34. */
  35. public static boolean setFileToBucket(String key, String localFilePath){
  36. Configuration cfg = new Configuration(QiniuUtil.zone);
  37. Auth auth = Auth.create(QiniuUtil.accessKey, QiniuUtil.secretKey);
  38. UploadManager uploadManager = new UploadManager(cfg);
  39. String upToken = auth.uploadToken(QiniuUtil.bucket);
  40. try {
  41. Response response = uploadManager.put(localFilePath, key, upToken);
  42. //解析上传成功的结果
  43. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  44. System.out.println(putRet.key);
  45. System.out.println(putRet.hash);
  46. return true;
  47. } catch (QiniuException ex) {
  48. Response r = ex.response;
  49. System.err.println(r.toString());
  50. try {
  51. System.err.println(r.bodyString());
  52. } catch (QiniuException ex2) {
  53. }
  54. return false;
  55. }
  56. }
  57. /**
  58. * 上传本地目录下所有文件到七牛云上
  59. * @param remoteFolder
  60. * @param localFolderPath
  61. */
  62. public static void setFilesToBucket(String remoteFolder, String localFolderPath) {
  63. Configuration cfg = new Configuration(QiniuUtil.zone);
  64. Auth auth = Auth.create(QiniuUtil.accessKey, QiniuUtil.secretKey);
  65. UploadManager uploadManager = new UploadManager(cfg);
  66. File file = new File(localFolderPath);
  67. File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
  68. if (files == null) {// 如果目录为空,直接退出
  69. return;
  70. }
  71. // 遍历,目录下的所有文件
  72. for (File f : files) {
  73. if (f.isFile()) {
  74. String key = f.getName();
  75. if (remoteFolder != null) {
  76. key = remoteFolder + key;
  77. }
  78. // 再上传文件
  79. String upToken = auth.uploadToken(QiniuUtil.bucket);
  80. try {
  81. Response res = uploadManager.put(f, key, upToken);
  82. System.out.println(res.bodyString());
  83. } catch (QiniuException e) {
  84. Response r = e.response;
  85. System.err.println(r.toString());
  86. try {
  87. System.err.println(r.bodyString());
  88. } catch (QiniuException ex2) {
  89. }
  90. }
  91. } else if (f.isDirectory()) {
  92. String key = f.getName() + "/";
  93. if (remoteFolder != null) {
  94. key = remoteFolder + key;
  95. }
  96. setFilesToBucket(key, f.getAbsolutePath());
  97. }
  98. }
  99. }
  100. /**
  101. * 测试七牛上传后,自动进行数据处理操作,并另存处理后的文件
  102. * @param domain 存储空间所对应的域名
  103. * @param file 上传文件
  104. */
  105. public static void testFops(String domain, File file) {
  106. //通过AK,SK创建Auth 对象
  107. Auth auth = Auth.create(QiniuUtil.accessKey, QiniuUtil.secretKey);
  108. Configuration cfg = new Configuration(QiniuUtil.zone);
  109. //上传对象
  110. UploadManager uploadMgr = new UploadManager(cfg);
  111. //私有队列
  112. String pipeline = "av-pipeline";
  113. //水印文字
  114. String wmText = UrlSafeBase64.encodeToString("Word For Test");
  115. //水印文字的颜色
  116. String wmFontColor = UrlSafeBase64.encodeToString("#FFFF00");
  117. //设置avthumb 接口
  118. StringBuffer ops = new StringBuffer("");
  119. ops.append("avthumb/mp4/wmText/" + wmText +"/wmGravityText/NorthEast/wmFontColor/" + wmFontColor);
  120. String saveAs = UrlSafeBase64.encodeToString(QiniuUtil.bucket + ":" + "new_" + file.getName());
  121. //通过管道符 "|" 拼接 saveas 接口, 保存 数据处理后的视频
  122. ops.append("|saveas/" + saveAs);
  123. //saveas 接口 需要签名 sign
  124. String sign = domain + "/" + file.getName() + "?" + ops.toString();
  125. String encodeSign = UrlSafeBase64.encodeToString(sign);
  126. ops.append("/sign/" + encodeSign);
  127. //指定 数据处理 的 上传策略, 当文件上传成功后,自定执行数据处理操作,即:ops 的接口,图片加水印,另存为 new_file.getName();
  128. StringMap putPolicy = new StringMap();
  129. putPolicy.put("persistentOps", ops.toString()) //数据处理接口及参数
  130. .put("persistentPipeline", pipeline); //私有数据处理队列
  131. //获取上传凭证, 包含上传策略
  132. String uploadToken = auth.uploadToken(QiniuUtil.bucket, file.getName(), 3600, putPolicy);
  133. try {
  134. //上传
  135. Response resp = uploadMgr.put(file, file.getName(), uploadToken);
  136. //查看结果
  137. System.out.println(resp.statusCode + ":" + resp.bodyString());
  138. } catch (QiniuException e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. }
  143. public static void main(String[] args) {
  144. QiniuUpload upload = new QiniuUpload();
  145. try {
  146. QiniuUpload.delete("head/13211064273/head.png");
  147. // QiniuUpload.refresh("http://scene3d.4dage.com/head/13211064273/head.png");
  148. } catch (QiniuException e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. }