七牛上傳圖片和二進制流方法
阿新 • • 發佈:2018-10-26
mini ram uuid 方法 throws rom ack 其他 try
package com.qlyd.aspmanager.common.qiniu;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import com.qlyd.aspmanager.common.uuid.UUIDTool;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* @author madebychina
* @date 2018/9/5 10:12
*/
public class WebFileUploadTool {
/**
* 七牛AK
*/
public static final String accessKey = "bLNti1fu58oi6ZAlIS0y49RyU00CdUpuajbc8GWj";
/**
* 七牛SK
*/
public static final String secretKey = "7zruUMyV_VqluxsjHQs0Ozb1GrkINmBmAV9GxlLQ";
/**
* 存儲空間名字
*/
public static final String bucket = "mini-app";
/**
* 七牛綁定的自定義域名
*/
public static final String BUCKET_HOST_NAME = "https://miniapp.qiluyidian.mobi";
/**
* 上傳web文件到七牛
*
* @param filePath 文件地址
* @return 七牛存儲地址
*/
public static String uploadWebFile(String filePath) {
String imgUrl = null;
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone0());
String key = UUIDTool.getUUID();
String extension = FileNameTool.getWebFileExtensionName(filePath);
if (extension.length() > 0) {
key += "." + FileNameTool.getWebFileExtensionName(filePath);
}
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
FetchRet fetchRet = bucketManager.fetch(filePath, bucket, key);
//七牛的實際存儲地址
imgUrl = BUCKET_HOST_NAME + "/" + fetchRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
return imgUrl;
}
public String uploadImg(FileInputStream file, String key) {
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone1());
//...其他參數參考類註釋
UploadManager uploadManager = new UploadManager(cfg);
//...生成上傳憑證,然後準備上傳
// String bucket = "oy09glbzm.bkt.clouddn.com";
//默認不指定key的情況下,以文件內容的hash值作為文件名
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file, key, upToken, null, null);
//解析上傳成功的結果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
String return_path = BUCKET_HOST_NAME + "/" + putRet.key;
return return_path;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 通過輸入流上傳
*
* @param inputStream
* @return
* @throws
*/
public static String upload(InputStream inputStream) {
String imgUrl = "";
Configuration cfg = new Configuration(Zone.zone0());
//創建上傳對象
UploadManager uploadManager = new UploadManager(cfg);
String key = UUIDTool.getUUID();
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(inputStream, key, upToken, null, null);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
imgUrl = BUCKET_HOST_NAME + "/" + putRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return imgUrl;
}
}
七牛上傳圖片和二進制流方法