1. 七牛云

对象存储OSS

https://developer.qiniu.com/kodo/1239/java 开发手册

2. 坐标

  1. <dependency>
  2. <groupId>com.qiniu</groupId>
  3. <artifactId>qiniu-java-sdk</artifactId>
  4. <version>[7.7.0, 7.7.99]</version>
  5. </dependency>

3. 获取Key

https://portal.qiniu.com/user/key

4. 上传文件

  1. package com.itheima.utils;
  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.BucketManager;
  7. import com.qiniu.storage.Configuration;
  8. import com.qiniu.storage.Region;
  9. import com.qiniu.storage.UploadManager;
  10. import com.qiniu.storage.model.DefaultPutRet;
  11. import com.qiniu.util.Auth;
  12. /**
  13. * 七牛云工具类
  14. */
  15. public class QiniuUtils {
  16. public static String accessKey = "OGqxqu61GWk26uprAF4MxTKWv-VErHNSMUHl5vNu";
  17. public static String secretKey = "YBeqVIDM9sfnLieKxg4KiRr_gYsmYA_Vf28H2Dgj";
  18. public static String bucket = "healthspace257";
  19. public static void upload2Qiniu(String filePath,String fileName){
  20. //构造一个带指定Zone对象的配置类
  21. Configuration cfg = new Configuration(Region.huanan());
  22. UploadManager uploadManager = new UploadManager(cfg);
  23. Auth auth = Auth.create(accessKey, secretKey);
  24. String upToken = auth.uploadToken(bucket);
  25. try {
  26. Response response = uploadManager.put(filePath, fileName, upToken);
  27. //解析上传成功的结果
  28. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  29. } catch (QiniuException ex) {
  30. Response r = ex.response;
  31. try {
  32. System.err.println(r.bodyString());
  33. } catch (QiniuException ex2) {
  34. //ignore
  35. }
  36. }
  37. }
  38. //上传文件
  39. public static void upload2Qiniu(byte[] bytes, String fileName){
  40. //构造一个带指定Zone对象的配置类
  41. Configuration cfg = new Configuration(Region.huanan());
  42. //...其他参数参考类注释
  43. UploadManager uploadManager = new UploadManager(cfg);
  44. //默认不指定key的情况下,以文件内容的hash值作为文件名
  45. String key = fileName;
  46. Auth auth = Auth.create(accessKey, secretKey);
  47. String upToken = auth.uploadToken(bucket);
  48. try {
  49. Response response = uploadManager.put(bytes, key, upToken);
  50. //解析上传成功的结果
  51. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  52. System.out.println(putRet.key);
  53. System.out.println(putRet.hash);
  54. } catch (QiniuException ex) {
  55. Response r = ex.response;
  56. System.err.println(r.toString());
  57. try {
  58. System.err.println(r.bodyString());
  59. } catch (QiniuException ex2) {
  60. //ignore
  61. }
  62. }
  63. }
  64. //删除文件
  65. public static void deleteFileFromQiniu(String fileName){
  66. //构造一个带指定Zone对象的配置类
  67. Configuration cfg = new Configuration(Region.huanan());
  68. String key = fileName;
  69. Auth auth = Auth.create(accessKey, secretKey);
  70. BucketManager bucketManager = new BucketManager(auth, cfg);
  71. try {
  72. bucketManager.delete(bucket, key);
  73. } catch (QiniuException ex) {
  74. //如果遇到异常,说明删除失败
  75. System.err.println(ex.code());
  76. System.err.println(ex.response.toString());
  77. }
  78. }
  79. }