我们在Android应用中使用Dropbox API v2将数据库文件(.db)上传到用户的Dropbox App文件夹。以下是上传文件的代码:
InputStream inputStream = null;
FileMetadata obj = null;
try {
inputStream = new FileInputStream(dbFile);
obj = mDbxClient.files().uploadBuilder(remoteFilePath)
.withMode(WriteMode.OVERWRITE)
.uploadAndFinish(inputStream);
} catch (DbxException | IOException e) {
obj = null;
mException = e;
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
同一文件快速连续上传3-4次。 现在,当互联网速度快(4G,Wifi)时,一切正常,但当用户切换到2G / 3G网络时,文件不能完全上传。调用onUploadComplete()的回调方法,但此处的文件大小已大大减少。我附上了数据库文件的版本历史记录。 https://www.dropbox.com/s/s7y43707ic1kqxg/dropbox.jpg?dl=0
您可以看到文件大小突然减少到104KB。这将成为一个损坏的数据库文件。
有什么方法可以解决这个问题吗? 我应该更改上传方法吗? (也许使用UploadSession() 要么 上传不应该快速连续调用? 请指导
编辑:
以下是对上传文件的调用:
AsyncTaskCompat.executeParallel(new DropboxUploadFileTask(DropboxClientFactory.getClient(), new DropboxUploadFileTask.Callback() {
@Override
public void onUploadComplete(FileMetadata result) {
String message = result.getName() + " size " + result.getSize() + " modified " +
java.text.DateFormat.getDateTimeInstance().format(result.getClientModified());
Log.i("BKSync upload ends: ", message);
}
@Override
public void onError(Exception e) {
Log.e("BKSync", "Failed to upload file.", e);
}
}, context), glDbName, "");
class DropboxUploadFileTask extends AsyncTask<String, Void, FileMetadata> {
private final DbxClientV2 mDbxClient;
private final Callback mCallback;
private Exception mException;
private Context mContext;
public interface Callback {
void onUploadComplete(FileMetadata result);
void onError(Exception e);
}
DropboxUploadFileTask(DbxClientV2 dbxClient, Callback callback, Context context) {
mDbxClient = dbxClient;
mCallback = callback;
mContext = context;
}
@Override
protected void onPreExecute() {
Log.e("BKSync", "upload starts");
}
@Override
protected void onPostExecute(FileMetadata result) {
Log.e("BKSync", "upload ends");
super.onPostExecute(result);
if (mException != null) {
mCallback.onError(mException);
} else if (result == null) {
mCallback.onError(null);
} else {
mCallback.onUploadComplete(result);
}
}
@Override
protected FileMetadata doInBackground(String... params) {
String dbFileName = params[0];
File dbFile = mContext.getDatabasePath(dbFileName);
String remoteFolderPath = params[1];
String remoteFileName = dbFile.getName();
InputStream inputStream = null;
FileMetadata obj = null;
try {
inputStream = new FileInputStream(dbFile);
obj = mDbxClient.files().uploadBuilder(remoteFolderPath + "/" + remoteFileName)
.withMode(WriteMode.OVERWRITE)
.uploadAndFinish(inputStream);
} catch (DbxException | IOException e) {
obj = null;
mException = e;
Log.e("BKSync", "exception1");
mException.printStackTrace();
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
Log.e("BKSync", "exception2");
e.printStackTrace();
}
}
return obj;
}
}
以下是日志:
05-29 12:08:06.322 11232-11232/com.bk E/BKSync: upload starts
05-29 12:08:13.129 11232-11232/com.bk E/BKSync: upload ends
05-29 12:08:13.130 11232-11232/com.bk I/BKSync upload ends:: demo2_local.db size 692224 modified May 29, 2018 12:08:13 PM
05-29 12:08:13.471 11232-11232/com.bk E/BKSync: upload starts
05-29 12:08:22.992 11232-11232/com.bk E/BKSync: upload ends
05-29 12:08:22.993 11232-11232/com.bk I/BKSync upload ends:: demo2_local.db size 692224 modified May 29, 2018 12:08:23 PM
05-29 12:08:45.181 11232-11232/com.bk E/BKSync: upload starts
05-29 12:08:48.692 11232-11232/com.bk E/BKSync: upload starts
05-29 12:08:49.137 11232-11232/com.bk E/BKSync: upload starts
05-29 12:08:51.962 11232-11232/com.bk E/BKSync: upload starts
f05-29 12:09:24.183 11232-30020/com.bk E/BKSync: exception1
05-29 12:09:24.184 11232-30020/com.bk W/System.err: com.dropbox.core.NetworkIOException: Unable to resolve host "content.dropboxapi.com": No address associated with hostname
at com.dropbox.core.DbxUploader.finish(DbxUploader.java:235)
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:106)
at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
05-29 12:09:24.185 11232-30020/com.bk W/System.err: at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:81)
at com.bkbk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
05-29 12:09:24.189 11232-30020/com.bk W/System.err: Caused by: java.net.UnknownHostException: Unable to resolve host "content.dropboxapi.com": No address associated with hostname
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:125)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at okhttp3.Dns$1.lookup(Dns.java:39)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
05-29 12:09:24.190 11232-30020/com.bk W/System.err: at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:213)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-29 12:09:24.193 11232-30020/com.bk W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-29 12:09:24.194 11232-30020/com.bk W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
05-29 12:09:24.195 11232-30020/com.bk W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
... 3 more
05-29 12:09:24.196 11232-30020/com.bk W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:106)
... 26 more
05-29 12:09:24.211 11232-11232/com.bk E/BKSync: upload ends
Failed to upload file.
05-29 12:09:24.300 11232-11232/com.bk E/BKSync: upload starts
05-29 12:09:24.314 11232-30020/com.bk E/BKSync: exception1
05-29 12:09:24.315 11232-30020/com.bk W/System.err: com.dropbox.core.NetworkIOException: Pipe closed
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:103)
at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:81)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
05-29 12:09:24.316 11232-30020/com.bk W/System.err: at java.lang.Thread.run(Thread.java:762)
Caused by: com.dropbox.core.util.IOUtil$WriteException: Pipe closed
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:61)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:68)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:43)
at com.dropbox.core.http.HttpRequestor$Uploader.upload(HttpRequestor.java:98)
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:98)
... 8 more
05-29 12:09:24.318 11232-30020/com.bk W/System.err: Caused by: java.io.IOException: Pipe closed
at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:267)
at java.io.PipedInputStream.receive(PipedInputStream.java:233)
at java.io.PipedOutputStream.write(PipedOutputStream.java:149)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:59)
... 12 more
05-29 12:09:24.441 11232-11232/com.bk E/BKSync: upload ends
05-29 12:09:24.442 11232-11232/com.bk E/BKSync: Failed to upload file.
com.dropbox.core.NetworkIOException: Pipe closed
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:103)
at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:81)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: com.dropbox.core.util.IOUtil$WriteException: Pipe closed
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:61)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:68)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:43)
at com.dropbox.core.http.HttpRequestor$Uploader.upload(HttpRequestor.java:98)
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:98)
at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:81)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.io.IOException: Pipe closed
at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:267)
at java.io.PipedInputStream.receive(PipedInputStream.java:233)
at java.io.PipedOutputStream.write(PipedOutputStream.java:149)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:59)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:68)
at com.dropbox.core.util.IOUtil.copyStreamToStream(IOUtil.java:43)
at com.dropbox.core.http.HttpRequestor$Uploader.upload(HttpRequestor.java:98)
at com.dropbox.core.DbxUploader.uploadAndFinish(DbxUploader.java:98)
at com.dropbox.core.v2.DbxUploadStyleBuilder.uploadAndFinish(DbxUploadStyleBuilder.java:92)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:81)
at com.bk.DropboxUploadFileTask.doInBackground(DropboxUploadFileTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
如您所见,应用程序引发的异常。 当用户开始在4G上传时,然后由于来电,网络切换到3G,就会出现问题。 Dropbox上的文件大小会大幅减少。
请帮忙。
答案 0 :(得分:0)
我在github上创建了一个问题:https://github.com/dropbox/dropbox-sdk-java/issues/191用于跟踪。它看起来像上传代码中的一个错误。但我们没有计划立即修复它并发布新版本。
所以我的建议是: