@Test
public void haha() {
//構造一個帶指定 Region 對象的配置類
Configuration cfg = new Configuration(Region.region2());
//...其他參數參考類注釋
UploadManager uploadManager = new UploadManager(cfg);
//...生成上傳憑證,然後準備上傳
String accessKey = "your key";
String secretKey = " you decret";
String bucket = "your 空間名字";
//預設不指定key的情況下,以檔案内容的hash值作為檔案名
String key = null;
try {
byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
System.out.println(ex2);
}
}
} catch (UnsupportedEncodingException ex) {
//ignore
System.out.println(ex);
}
}
導 包
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
注意點:
//構造一個帶指定Zone對象的配置類
Configuration cfg = new Configuration(Zone.zone0());//Zone.zone0() 指華東
Configuration cfg = new Configuration(Zone.zone1());//華北
Configuration cfg = new Configuration(Zone.zone2());//華南
上傳本地檔案并且生成外鍊:
private String path = "C:\\Users\\ASUS\\Desktop\\課表.jpg";
@Test
public void haha() {
//構造一個帶指定 Region 對象的配置類
Configuration cfg = new Configuration(Region.region2());
//...其他參數參考類注釋
UploadManager uploadManager = new UploadManager(cfg);
//...生成上傳憑證,然後準備上傳
String accessKey = "yours key";
String secretKey = "yours key";
String bucket = "ywf";
//預設不指定key的情況下,以檔案内容的hash值作為檔案名
String key = null;
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(path, key, upToken);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
System.out.println(ex2);
}
}
} catch (UnsupportedEncodingException ex) {
//ignore
System.out.println(ex);
}
}
圖檔伺服器 會給你一個域名,将 域名+ ret.hash
就是圖檔的通路外鍊了。
@Override
public String saveImageWithPath(MultipartFile file) {
if(!checkSize(file)) {
return "ERROR";
}
Configuration cfg = new Configuration(Region.region0());//華東的倉庫
//...其他參數參考類注釋
UploadManager uploadManager = new UploadManager(cfg);
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(buckName);
String result=null;
try {
Response response = uploadManager.put(file.getBytes(), key, upToken);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
// System.out.println(putRet.key);
// System.out.println(putRet.hash);
log.info(putRet.key);
result = putRet.hash;
} catch (QiniuException ex) {
Response r = ex.response;
log.error(r.toString());
try {
log.error(r.bodyString());
} catch (QiniuException ex2) {
//ignore
log.error(String.valueOf(ex2));
}
} catch (IOException e) {
log.error(e.getMessage());
}
//圖檔路徑: 域名+'/'+ 具體的hash值
return prefix+'/'+result;
}