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
Q #1) Why are downloads so slow? Answer: There are various reasons that affect the downloading of files in a system. Some common reasons are slow internet speed, excessive cache memory, hardware issues, and modem firmware errors.
Throttling occurs when the carrier purposefully slows down the speed of data downloads on your network. There are a few reasons this might happen: There's too much traffic on the tower or network and the provider is trying to ensure everyone has at least some access for basic use.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With