1、引入jar
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.3</version>
</dependency>
2、上传文件
package com.hui.qiniu;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import java.io.IOException;
import java.util.UUID;
/**
* 上传到七牛返回 hash 和 key
*/
public class UploadDemo {
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
private static final String BUCKET_NAME = "myhui";
public static void main(String args[]) throws IOException {
new UploadDemo().upload();
}
public String getUpToken() {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
return auth.uploadToken(BUCKET_NAME);
}
public void upload() throws IOException {
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
UploadManager uploadManager = new UploadManager(c);
String key = "image/test/" + UUID.randomUUID() + ".png";
String FilePath = "D://1.jpg";
try {
Response res = uploadManager.put(FilePath, key, getUpToken());
System.out.println(res.bodyString());
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.bodyString());
}
}
}
3、列出文件
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
/**
* 文件列表
*/
public class ListDemo {
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
public static void main(String args[]) {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
Zone z = Zone.zone0();
Configuration c = new Configuration(z);
BucketManager bucketManager = new BucketManager(auth, c);
String bucket = "myhui";
try {
FileListing fileListing = bucketManager.listFiles(bucket, null, null, 1000, null);
FileInfo[] items = fileListing.items;
for (FileInfo fileInfo : items) {
System.out.println(fileInfo.key);
}
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.toString());
}
}
}
4、获取文件信息
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Json;
/**
* 获取文件的hash 和key 实际上是一个没有什么卵用的方法
*/
public class FetchDemo {
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
public static void main(String args[]) {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
Zone z = Zone.zone0();
Configuration c = new Configuration(z);
BucketManager bucketManager = new BucketManager(auth, c);
String bucket = "myhui";
String key = "ce81d939-437e-45ec-8580-13913cd1264c.png";
String url = "http://ojg32ej8x.bkt.clouddn.com/ce81d939-437e-45ec-8580-13913cd1264c.png";
try {
DefaultPutRet fetch = bucketManager.fetch(url, bucket, key);
System.out.println("fetch = " + Json.encode(fetch));
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.toString());
}
}
}
5、上传成功之后,回调方法
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import java.io.IOException;
import java.util.UUID;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
public class UploadCallBackDemo {
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
private static final String BUCKET_NAME = "myhui";
String key = "image/test/" + UUID.randomUUID() + ".png";
String FilePath = "D://1.jpg";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
UploadManager uploadManager = new UploadManager(c);
public String getUpToken() {
return auth.uploadToken(BUCKET_NAME, null, 3600, new StringMap()
.put("callbackUrl", "http://your.domain.com/callback")
.put("callbackBody", "filename=$(fname)&filesize=$(fsize)"));
}
public void upload() throws IOException {
try {
Response res = uploadManager.put(FilePath, key, getUpToken());
System.out.println(res.bodyString());
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.bodyString());
}
}
public static void main(String args[]) throws IOException {
new UploadCallBackDemo().upload();
}
}