@Test public void testUpload() { QNUploader uploader = new QNUploader(); try { String ak = ""; String sk = ""; String bucket = "temp"; File f = new File("pom.xml"); Zone zone = null; UploadInfo info = uploader.upload("testqq", f, ak, sk, bucket, zone); System.out.println(info); assertEquals("testqq/pom.xml", info.key); assertEquals("temp", info.bucket); assertEquals(f.length(), info.fsize); assertEquals("http: info.getUrl("http: AuthInfo auth = uploader.getToken("testqq", "", "", "", "pom.xml"); System.out.println(auth); assertNotNull(auth); assertNotNull(auth.token); assertEquals("http: assertEquals(1, auth.days); assertTrue((8 << 20) >= auth.limit); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
public UploadInfo upload(String qq, File file, String ak, String sk, String bucket, Zone zone) throws Exception { String key = String.format("%s/%s", qq, file.getName()); AuthInfo authInfo = getToken(qq, ak, sk, bucket, key); System.out.println(authInfo); if (authInfo.limit > 0 && file.length() > authInfo.limit) { throw new RuntimeException("今日上传流量不足，剩余流量：" + authInfo.limit); } Configuration cfg = new Configuration( zone == null ? Zone.autoZone() : zone); UploadManager uploadManager = new UploadManager(cfg); com.qiniu.http.Response response = uploadManager .put(file.getAbsolutePath(), key, authInfo.token); if (!response.isOK()) { throw new RuntimeException( response.error + "(code=" + response.statusCode + ")"); } UploadInfo putRet = new Gson().fromJson(response.bodyString(), UploadInfo.class); if (authInfo.domain != null && !authInfo.domain.isEmpty()) { putRet.domain = authInfo.domain; } if (authInfo.limit > 0) { callback(qq, putRet); } return putRet; }
QNUploader { public UploadInfo upload(String qq, File file, String ak, String sk, String bucket, Zone zone) throws Exception { String key = String.format("%s/%s", qq, file.getName()); AuthInfo authInfo = getToken(qq, ak, sk, bucket, key); System.out.println(authInfo); if (authInfo.limit > 0 && file.length() > authInfo.limit) { throw new RuntimeException("今日上传流量不足，剩余流量：" + authInfo.limit); } Configuration cfg = new Configuration( zone == null ? Zone.autoZone() : zone); UploadManager uploadManager = new UploadManager(cfg); com.qiniu.http.Response response = uploadManager .put(file.getAbsolutePath(), key, authInfo.token); if (!response.isOK()) { throw new RuntimeException( response.error + "(code=" + response.statusCode + ")"); } UploadInfo putRet = new Gson().fromJson(response.bodyString(), UploadInfo.class); if (authInfo.domain != null && !authInfo.domain.isEmpty()) { putRet.domain = authInfo.domain; } if (authInfo.limit > 0) { callback(qq, putRet); } return putRet; } }
QNUploader { public UploadInfo upload(String qq, File file, String ak, String sk, String bucket, Zone zone) throws Exception { String key = String.format("%s/%s", qq, file.getName()); AuthInfo authInfo = getToken(qq, ak, sk, bucket, key); System.out.println(authInfo); if (authInfo.limit > 0 && file.length() > authInfo.limit) { throw new RuntimeException("今日上传流量不足，剩余流量：" + authInfo.limit); } Configuration cfg = new Configuration( zone == null ? Zone.autoZone() : zone); UploadManager uploadManager = new UploadManager(cfg); com.qiniu.http.Response response = uploadManager .put(file.getAbsolutePath(), key, authInfo.token); if (!response.isOK()) { throw new RuntimeException( response.error + "(code=" + response.statusCode + ")"); } UploadInfo putRet = new Gson().fromJson(response.bodyString(), UploadInfo.class); if (authInfo.domain != null && !authInfo.domain.isEmpty()) { putRet.domain = authInfo.domain; } if (authInfo.limit > 0) { callback(qq, putRet); } return putRet; } QNUploader(); }
QNUploader { public UploadInfo upload(String qq, File file, String ak, String sk, String bucket, Zone zone) throws Exception { String key = String.format("%s/%s", qq, file.getName()); AuthInfo authInfo = getToken(qq, ak, sk, bucket, key); System.out.println(authInfo); if (authInfo.limit > 0 && file.length() > authInfo.limit) { throw new RuntimeException("今日上传流量不足，剩余流量：" + authInfo.limit); } Configuration cfg = new Configuration( zone == null ? Zone.autoZone() : zone); UploadManager uploadManager = new UploadManager(cfg); com.qiniu.http.Response response = uploadManager .put(file.getAbsolutePath(), key, authInfo.token); if (!response.isOK()) { throw new RuntimeException( response.error + "(code=" + response.statusCode + ")"); } UploadInfo putRet = new Gson().fromJson(response.bodyString(), UploadInfo.class); if (authInfo.domain != null && !authInfo.domain.isEmpty()) { putRet.domain = authInfo.domain; } if (authInfo.limit > 0) { callback(qq, putRet); } return putRet; } QNUploader(); AuthInfo getToken(String qq, String ak, String sk, String bucket,
            String key); UploadInfo upload(String qq, File file, String ak, String sk,
            String bucket, Zone zone); }
QNUploader { public UploadInfo upload(String qq, File file, String ak, String sk, String bucket, Zone zone) throws Exception { String key = String.format("%s/%s", qq, file.getName()); AuthInfo authInfo = getToken(qq, ak, sk, bucket, key); System.out.println(authInfo); if (authInfo.limit > 0 && file.length() > authInfo.limit) { throw new RuntimeException("今日上传流量不足，剩余流量：" + authInfo.limit); } Configuration cfg = new Configuration( zone == null ? Zone.autoZone() : zone); UploadManager uploadManager = new UploadManager(cfg); com.qiniu.http.Response response = uploadManager .put(file.getAbsolutePath(), key, authInfo.token); if (!response.isOK()) { throw new RuntimeException( response.error + "(code=" + response.statusCode + ")"); } UploadInfo putRet = new Gson().fromJson(response.bodyString(), UploadInfo.class); if (authInfo.domain != null && !authInfo.domain.isEmpty()) { putRet.domain = authInfo.domain; } if (authInfo.limit > 0) { callback(qq, putRet); } return putRet; } QNUploader(); AuthInfo getToken(String qq, String ak, String sk, String bucket,
            String key); UploadInfo upload(String qq, File file, String ak, String sk,
            String bucket, Zone zone); static String API_URL; static String API_GET_TOKEN; static String API_CALLBACK; }