forked from qiniu/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.java
More file actions
70 lines (59 loc) · 2.28 KB
/
Copy pathupload.java
File metadata and controls
70 lines (59 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
public class UploadDemo {
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "Access_Key";
String SECRET_KEY = "Secret_Key";
//要上传的空间
String bucketname = "Bucket_Name";
//上传到七牛后保存的文件名
String key = "my-java.png";
//上传文件的路径
String FilePath = "/.../...";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
///////////////////////指定上传的Zone的信息//////////////////
//第一种方式: 指定具体的要上传的zone
//注:该具体指定的方式和以下自动识别的方式选择其一即可
//要上传的空间(bucket)的存储区域为华东时
// Zone z = Zone.zone0();
//要上传的空间(bucket)的存储区域为华北时
// Zone z = Zone.zone1();
//要上传的空间(bucket)的存储区域为华南时
// Zone z = Zone.zone2();
//第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
Zone z = Zone.autoZone();
Configuration c = new Configuration(z);
//创建上传对象
UploadManager uploadManager = new UploadManager(c);
public static void main(String args[]) throws IOException {
new UploadDemo().upload();
}
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
public String getUpToken() {
return auth.uploadToken(bucketname);
}
public void upload() throws IOException {
try {
//调用put方法上传
Response res = uploadManager.put(FilePath, key, getUpToken());
//打印返回的信息
System.out.println(res.bodyString());
} catch (QiniuException e) {
Response r = e.response;
// 请求失败时打印的异常的信息
System.out.println(r.toString());
try {
//响应的文本信息
System.out.println(r.bodyString());
} catch (QiniuException e1) {
//ignore
}
}
}
}