通过网络/本地主机发送大文件

发布于 2025-01-05 03:03:04 字数 1051 浏览 0 评论 0原文

这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳絮泡泡 2025-01-12 03:03:04

这是让它发挥作用的原因:

while (true) {
  synchronized (buffer) {
    int amountRead = file.read(buffer);
    if (amountRead == -1) {
      break;
    }
    out.write(buffer, 0, amountRead);
  }
}

Here's what made it work:

while (true) {
  synchronized (buffer) {
    int amountRead = file.read(buffer);
    if (amountRead == -1) {
      break;
    }
    out.write(buffer, 0, amountRead);
  }
}
野生奥特曼 2025-01-12 03:03:04

使用多部分 POST。像这样的东西

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,null); 
entity.addPart("File", new FileBody (new File(FILE_PATH), MIME_TYPE));
httppost.setEntity(entity); 
HttpResponse response = httpclient.execute(httppost); 
return response;

Use Multipart POST. Something like

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,null); 
entity.addPart("File", new FileBody (new File(FILE_PATH), MIME_TYPE));
httppost.setEntity(entity); 
HttpResponse response = httpclient.execute(httppost); 
return response;
有木有妳兜一样 2025-01-12 03:03:04

为此使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文