package qiNiu;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import cn.itcast.commons.CommonUtils;
public class QiniuCloudUtil {
//设置七牛云账号AK和SK
private static final String ACCESS_KEY="xxxxx填自己的AK";
private static final String SECRET_KEY="xxxxx填自己的SK";
//要上传的空间bucket
private static final String BUCKET="填自己的要上传的空间";
//外链地址:改成自己的域名
private static final String DOMAIN="填自己的域名";
/*
* 获取七牛云Token
*/
public static String getupToken() {
//密钥配置上传的凭证
Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
StringMap putPolicy = new StringMap();
putPolicy.put("returnBody", "{\"fileUrl\": \""+DOMAIN+"$(key)\"}");//上传成功后返回的json
long expireSeconds = 3600;//过期时间
String upToken = auth.uploadToken(BUCKET, null, expireSeconds, putPolicy);
System.out.println(upToken);
return upToken;
}
/**上传本地文件
* @param 文件名
* @return 外链地址
* */
public static String uploadFile(String localFilePath){
//文件外链地址
StringBuffer fileUrl=new StringBuffer(DOMAIN);
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
//如果是Windows情况下,格式是 D:\\qiniu\\test.png
//String localFilePath = "D:\\IMG_3931.png";
//默认不指定key的情况下,以文件内容的hash值作为文件名
//指定了key:http://q2yl1ub9s.bkt.clouddn.com/EA3613993A604422A6874095DF94F903
String key = CommonUtils.uuid();
try {
Response response = uploadManager.put(localFilePath, key, getupToken());
//解析上传成功的结果
//DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//System.out.println(putRet.key);
// System.out.println(putRet.hash);
fileUrl.append("/"+key);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
return fileUrl.toString();
}
public void test1(){
System.out.println(uploadFile("D:\\upload.jpg"));
}
/**
* 字节数组上传
* @param uploadBytes 字节数组
* @param fileName 文件名称
* @return 外链
*/
public static String uploadFileBytes(byte[] uploadBytes,String fileName){
//文件外链地址
StringBuffer fileUrl=new StringBuffer(DOMAIN);
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
try {
Response response = uploadManager.put(uploadBytes, key, getupToken());
fileUrl.append("/"+key);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
return fileUrl.toString();
}
public void test2() throws UnsupportedEncodingException{
System.out.println(uploadFileBytes("hello qiniu cloud".getBytes("utf-8"),CommonUtils.uuid()));
}
/**
* //数据流上传
* @param byteInputStream
* @param fileName
* @return
*/
public static String uploadFileStream(InputStream inputStream,String fileName){
//文件外链地址
StringBuffer fileUrl=new StringBuffer(DOMAIN);
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.region2());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = fileName;
// byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
// ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
try {
Response response = uploadManager.put(inputStream,key,getupToken(),null, null);
fileUrl.append("/"+key);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
return fileUrl.toString();
}
@Test
public void test3() throws UnsupportedEncodingException{
System.out.println(
uploadFileStream(new ByteArrayInputStream("hello qiniu cloud23232323".getBytes("utf-8")),CommonUtils.uuid())
);
}
}
七牛云:上传文件并返回url
最新推荐文章于 2023-02-07 14:18:34 发布