我想實現分塊上傳到Dropbox。 我已經根據Dropbox API文檔中的示例編寫了代碼,但失敗了。Dropbox ChunkedUpload
下面的代碼拋出異常:
com.dropbox.client2.exception.DropboxIOException:阿帕奇了HTTPClient 遇到一個錯誤。沒有反應,再試一次。 at com.dropbox.client2.RESTUtility.execute(Unknown Source)at com.dropbox.client2.DropboxAPI $ ChunkedUploadRequest.upload(Unknown Source)at com.dropbox.client2.DropboxAPI $ ChunkedUploader.upload(Unknown Source) 在com.dropbox.client2.DropboxAPI $ ChunkedUploader.upload(未知 來源)
下面是代碼:
/** Uploads content to Dropbox.
* @param dropbox An already linked dropbox API
* @param stream The stream to upload
* @param length The number of bytes to upload
* @param path The path where to store the uploaded content
* @return a boolean, false if the upload was cancelled, true if it completes
* @throws IOException
* @throws DropboxException
*/
public static boolean upload(DropboxAPI<?> dropbox, InputStream stream,
long length, String path) throws IOException, DropboxException {
// Create a chunked uploader
ChunkedUploader uploader = dropbox.getChunkedUploader(stream, length);
try {
int tryCounter = 1;
while (!uploader.isComplete()) {
try {
uploader.upload(); // The exception occurs on this line
} catch (DropboxException e) {
tryCounter++;
if (tryCounter > 5) throw e; // Give up after a while.
System.err.println ("Upload failed, trying again ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
}
}
} catch (DropboxPartialFileException e) {
// Upload was cancelled
return false;
}
// If the upload completes, determine the current revision of the path.
String parentRev = null;
try {
Entry metadata = dropbox.metadata(path, 1, null, false, null);
parentRev = metadata.rev;
} catch (DropboxServerException e) {
// If the error is not a FileNotFound error => It's really an error
if (e.error!=DropboxServerException._404_NOT_FOUND) throw e;
}
// Finish the upload
uploader.finish(path, parentRev);
return true;
}
什麼我做錯了任何想法?
PS:當然,我驗證了其他api函數可以與我傳遞給該方法的DropboxAPI實例一起使用。
嗨,我剛剛在我自己的項目中使用你的代碼,它正常工作與複製粘貼,所以它沒有聽到,可能是相對於帳戶步驟或初始化您的「Dropbox」對象的鏈接DropboxAPI)? –
pommedeterresautee