通过网络/本地主机发送大文件
这是代码:
private void sendFile(InputStream file, OutputStream out) throws IOException {
Log.d(TAG, "trying to send file...");
final int buffer_size = 4096;
try {
byte[] bytes = new byte[buffer_size];
while(true) {
int count = file.read(bytes, 0, buffer_size);
if (count == -1) {
break;
}
out.write(bytes, 0, count);
Log.d("copystream", bytes + "");
}
} catch (Exception e) {
Log.e("copystream", "exception caught while sending file... " + e.getMessage());
}
}
我试图通过输出流(OutputStream out)发送一个大文件(InputStream 文件)。这段代码适用于较小的文件,但对于 5mb 及以上的文件(我没有对限制进行基准测试),它只是在一段时间后冻结,没有错误或任何东西。
Log.d("copystream", bytes + "");
会输出一段时间,但最终会停止记录。
Log.e("copystream", "发送文件时捕获异常..." + e.getMessage());
永远不会显示。
这是一个更大的代码库的一部分,该代码库实际上是一个在 Android 设备上运行的文件服务器。
有什么想法吗?
Here's the code:
private void sendFile(InputStream file, OutputStream out) throws IOException {
Log.d(TAG, "trying to send file...");
final int buffer_size = 4096;
try {
byte[] bytes = new byte[buffer_size];
while(true) {
int count = file.read(bytes, 0, buffer_size);
if (count == -1) {
break;
}
out.write(bytes, 0, count);
Log.d("copystream", bytes + "");
}
} catch (Exception e) {
Log.e("copystream", "exception caught while sending file... " + e.getMessage());
}
}
I'm trying to send a large File (InputStream file) over an output stream (OutputStream out). This code works for smaller files, but for something like 5mb and above (I haven't benchmarked the limit), it just freezes after sometime without error or anything.
Log.d("copystream", bytes + "");
would output for some time, but will eventually stop logging.
Log.e("copystream", "exception caught while sending file... " + e.getMessage());
never shows.
This is part of a larger codebase which is actually a file server that runs on the Android device.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是让它发挥作用的原因:
Here's what made it work:
使用多部分 POST。像这样的东西
Use Multipart POST. Something like
为此使用 AsyncTask 类,这里是例如链接
http://developer.android.com/reference/android/os/AsyncTask.html
Use AsyncTask Class for this, here is link for example
http://developer.android.com/reference/android/os/AsyncTask.html