为什么使用Java下载文件速度这么慢

发布于 2024-11-08 18:41:46 字数 649 浏览 4 评论 0原文

我编写了简单的 Java 下载器,但在速度方面存在一些问题。

一开始,速度还可以——就像我使用浏览器下载这个文件一样。但一段时间后,速度下降很多,每两秒改变一次 - 从 42kb/s 到 64kb/s,从 64kb/s 到 42kb/s。

我的代码:

InputStream is = null;
FileOutputStream os = null;
os = new FileOutputStream(...);
URL u = new URL(...);
URLConnection uc = u.openConnection();
is = uc.getInputStream();
final byte[] buf = new byte[1024];
for(int count = is.read(buf);count != -1;count = is.read(buf)) {
    os.write(buf, 0, count);
}

我应该怎样做才能最大限度地提高下载速度?

更新

文件大小从 1 MB 到大约 100 MB 不等。 我将缓冲区增加到 65536,结果是一样的。

关于测量:我每 3 秒检查一次写入了多少字节,然后将其除以 3 和 1024 - 它给出了 kb/s

I wrote simple Java Downloader and I have some problems with speed.

At first, speed is OK - just like when I use my browser to download this file. But after a while speed decreases a lot and change every two seconds - from 42kb/s to 64kb/s and from 64kb/s to 42kb/s.

My code:

InputStream is = null;
FileOutputStream os = null;
os = new FileOutputStream(...);
URL u = new URL(...);
URLConnection uc = u.openConnection();
is = uc.getInputStream();
final byte[] buf = new byte[1024];
for(int count = is.read(buf);count != -1;count = is.read(buf)) {
    os.write(buf, 0, count);
}

What should I do to maximalise speed of download?

UPDATE

Sizes of files are various from 1 to about 100MB.
I increased the buffer to 65536 to it is the same.

About measuring : I check every 3 second how many bytes was written, and then divide it by 3 and by 1024 - it gives me kb / s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

马蹄踏│碎落叶 2024-11-15 18:41:46

为了提高速度,在带宽和服务器容量的限制范围内,应用程序应该在多线程代码中使用多个连接(不止一个):每个线程创建自己的连接并查询文件的部分内容。

此类应用程序的一个示例是 IBM downloaddirector,它通常使用三个 HTTP 连接来检索大文件。大多数 FTP 客户端还能够使用多个连接来提高吞吐量。在Java中,可以使用Apache HttpClient来编写这样的多线程应用程序。

您没有详细说明您的 URL 中使用的协议。如果是 HTTP,则 HEAD 请求返回文件长度,并使用具有分块支持的 GET 来查询文件部分。

如果直接使用 HttpURLConnection 并设置 ChunkedStreamingMode 的值,即使使用单个连接,也可能获得更好的性能。

如果仍然不满意,请提供其他详细信息:

  • 您的“但是过了一会儿”是什么意思,您是否按顺序下载了许多文件?
  • 协议是什么?您使用特定的 URLStreamHandler 吗?
  • 你检查过 JVM 内存和垃圾收集使用情况吗?
  • 您的工作站可能正忙于做其他事情:CPU 使用率、网络带宽被其他操作使用、防病毒软件减慢磁盘访问速度?
  • 您是否通过代理:某些 HTTP 代理可能会在同一连接上几分钟后减少带宽...

To increase speed, up to the limit of your bandwidth and server capacity, an application should use several connections (more than only one) with multi-threaded code: each thread creates its own connection and queries for parts of the file.

An example of such an application is IBM download director which often uses three HTTP connections to retrieve large files. Most FTP clients are also able to work with multiple connections to increase throughput. In Java, Apache HttpClient may be used to write such a multi-threaded application.

You have not detailed the protocol used in your URL. If HTTP, a HEAD request returns the file length and GET with chunking support is used to query for file parts.

Probably you can get better performance even with a single connection if you directly use HttpURLConnection and set value for ChunkedStreamingMode.

If still unsatisfied, please provide additional details:

  • what your "But after a while" means, do you download many files in sequence ?
  • what is the protocol ? do you use a specific URLStreamHandler ?
  • have you checked your JVM memory and garbage collection usage ?
  • your workstation may be busy doing something else: CPU used, network bandwidth used by something else, anti-virus slowing down disk access ?
  • do you pass through a proxy: some HTTP proxy may reduce bandwidth after some minutes on the same connection...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文