@Test public void testUpload2() { QNUploader uploader = new QNUploader(); String ak = ""; String sk = ""; String bucket = ""; File f = new File("C:\\Users\\Jamling\\Desktop\\a.txt"); Zone zone = null; try { UploadInfo info = uploader.upload("157250921", f, ak, sk, bucket, zone); System.out.println(info); } catch (Exception e) { e.printStackTrace(); } }
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; }