七牛云存储java上传图片

1、注册七牛云存储账号

2、添加对象存储

名字随便取,存储区域随便选,空间分为公开空间和私有空间,可以都建一个,普通图片可以上传到公开空间,身份证银行卡等保密信息可以上传到私有空间

 

3、新建maven项目

pom.xml中添加

<dependency>

  <groupId>com.qiniu</groupId>

  <artifactId>qiniu-java-sdk</artifactId>

  <version>[7.2.0, 7.2.99]</version>

</dependency>

4、文件上传及获取url代码

package SevenCow;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

 

import com.google.gson.Gson;

import com.qiniu.common.QiniuException;

import com.qiniu.common.Zone;

import com.qiniu.http.Response;

import com.qiniu.storage.Configuration;

import com.qiniu.storage.UploadManager;

import com.qiniu.storage.model.DefaultPutRet;

import com.qiniu.util.Auth;

 

/**

 * 上传下载图片

 *

 * @author fcc

 *

 */

public class SevenCow {

 

private SevenCow() {

}

 

// Json类型转换

private final static Gson gson = new Gson();

 

// 公开空间名

private final static String bucketPublic = "输入你的公开空间名";

// 私有空间名

private final static String bucketPrivate = "输入你的私有空间名";

 //域名在产品列表——空间概览里

// 公开空间域名

private final static String domainOfBucketPublic = "输入你的公开空间域名";

// 私有空间域名

private final static String domainOfBucketPrivte = "输入你的私有空间域名";

 

// 私有空间的文件查看有效时间

//这个没有生效,提交七牛工单后回答:由于测试域名的过期时间是默认30天,请绑定自定义域名进行使用

private final static long defaultExpireInSeconds = 60 * 3;

 //个人中心——秘钥管理里查看你的accessKey和secretKey

private static String accessKey = "输入你的accessKey ";

private static String secretKey = "输入你的secretKey ";

 

private static UploadManager uploadManager = null;

private static Auth auth = Auth.create(accessKey, secretKey);

 

static {

//机房 Zone对象

//华东 Zone.zone0()

//华北 Zone.zone1()

//华南 Zone.zone2()

//北美 Zone.zoneNa0()

Configuration cfg = new Configuration(Zone.zone0());

uploadManager = new UploadManager(cfg);

}

 

/**

 * 获取公有空间域名

 *

 * @return

 */

public static String getDomainOfBucketPublic() {

return domainOfBucketPublic;

}

 

/**

 * 获取私有空间域名

 *

 * @return

 */

public static String getDomainOfBucketPrivte() {

return domainOfBucketPrivte;

}

 

/**

 * 获取上传文件到公有空间的token

 *

 * @return

 */

public static String getupTokenToPublic() {

return auth.uploadToken(bucketPublic);

}

 

/**

 * 获取上传文件到私有空间的token

 *

 * @return

 */

public static String getupTokenToPrivate() {

return auth.uploadToken(bucketPrivate);

}

 

/**

 * 根据文件路径上传到公有空间

 *

 * @param localFilePath

 * @return

 * @throws QiniuException

 */

public static String uploadFileToPublic(String localFilePath)

throws QiniuException {

String upToken = auth.uploadToken(bucketPublic);

Response response = uploadManager.put(localFilePath, null, upToken);

DefaultPutRet putRet = gson.fromJson(response.bodyString(),

DefaultPutRet.class);

return putRet.key;

}

 

/**

 * 根据文件路径上传到私有空间

 *

 * @param localFilePath

 * @return

 * @throws QiniuException

 */

public static String uploadFileToPirvate(String localFilePath)

throws QiniuException {

String upToken = auth.uploadToken(bucketPrivate);

Response response = uploadManager.put(localFilePath, null, upToken);

DefaultPutRet putRet = gson.fromJson(response.bodyString(),

DefaultPutRet.class);

return putRet.key;

}

 

/**

 * 根据二进制数组上传到公有空间

 *

 * @param bucket

 * @param uploadBytes

 * @return

 * @throws QiniuException

 */

public static String uploadBytesPublic(byte[] uploadBytes)

throws QiniuException {

String upToken = auth.uploadToken(bucketPublic);

Response response = uploadManager.put(uploadBytes, null, upToken);

DefaultPutRet putRet = gson.fromJson(response.bodyString(),

DefaultPutRet.class);

return putRet.key;

}

 

/**

 * 根据二进制数组上传到私有空间

 *

 * @param bucket

 * @param uploadBytes

 * @return

 * @throws QiniuException

 */

public static String uploadBytesPrivate(byte[] uploadBytes)

throws QiniuException {

String upToken = auth.uploadToken(bucketPrivate);

Response response = uploadManager.put(uploadBytes, null, upToken);

DefaultPutRet putRet = gson.fromJson(response.bodyString(),

DefaultPutRet.class);

return putRet.key;

}

 

/**

 * 公开空间文件下载

 *

 * @throws UnsupportedEncodingException

 */

public static String downloadPublicFile(String fileName)

throws UnsupportedEncodingException {

String encodedFileName = URLEncoder.encode(fileName, "utf-8");

return String.format("%s/%s", domainOfBucketPublic, encodedFileName);

}

 

/**

 * 私有空间文件下载

 *

 * @param fileName

 * @return

 * @throws UnsupportedEncodingException

 */

public static String downloadPrivateFile(String fileName)

throws UnsupportedEncodingException {

return downloadPrivateFile(fileName, defaultExpireInSeconds);

}

 

/**

 * 私有空间文件下载

 *

 * @throws UnsupportedEncodingException

 */

public static String downloadPrivateFile(String fileName,

long expireInSeconds) throws UnsupportedEncodingException {

String encodedFileName = URLEncoder.encode(fileName, "utf-8");

String publicUrl = String.format("%s/%s", domainOfBucketPrivte,

encodedFileName);

String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);

return finalUrl;

}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值