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_KEYSECRET_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
}
}
}
}