Java multi-threaded download with resumable upload function jar

Source: Internet
Author: User

In the past two days, I sorted out the originally written multi-thread download program. Considering that all the original files are prose files, it is not convenient to use them. Therefore, I decided to write them into jar files, which is convenient to use. In addition, you can use XML to save the downloaded file for next download. It also fixes some bugs. As long as your computer permits, you can download as many downloads as you want.

Here I have an example. Because JDOM is used to process XML files, we want to package the JDOM used to downfile. Jar (: keystore) (the jar after the first correction ).(Failed again ))But I tried several times and several methods won't work, so you have to configure JDOM first, and then put my downfile. jar is also placed in the class path, and you can see the effect by running it directly. an XML file is generated under the directory where the file is saved and downloaded. the file cannot be automatically deleted, it can be deleted or not deleted, and does not affect the running of the program. This file is mainly used to facilitate resumable data transfer. The following is an example source code, which is very simple:

Import com. downfile .*;

/*

* Configure JDOM and downfile. jar first.

* By: http://blog.csdn.net/fenglibing

* If you want to hear your comments, there must be many shortcomings. If you need a source program, please contact me.

*/

Public class downfiletest {

Public downfiletest (){

}

 

Public static void main (string [] ARGs ){

String urlfile; // network address

Int threadnum; // Number of File Download threads

String localaddress; // local address

Urlfile = "http://www.netbox.cn: 88/download/nbsetup. EXE ";

// Currently, only one to nine threads can be processed, and later versions will be added.

Threadnum = 9;

// Note: The Path format must be written in the following format. The best path title cannot be missing.

Localaddress = "D: // multidowntest //"; // This folder is automatically created if it does not exist.

// Here we use three software to download at the same time. Of course, you can download more, which is so convenient to use.

Downfile = new downfile (urlfile, threadnum, localaddress );

Downfile. startdownfile ();

Urlfile = "http://dl.360safe.com/setup.exe ";

Downfile downfile1 = new downfile (urlfile, threadnum, localaddress );

Downfile1.startdownfile ();

Urlfile = "http://down.sandai.net/Thunder5.6.8.329.exe ";

Downfile downfile2 = new downfile (urlfile, threadnum, localaddress );

Downfile2.startdownfile ();

 

}

}

--------------------------------------------------
Bug fixes:

Bug, error analysis and solution:

1. An error occurs, indicating that the file cannot be found.
The reason is that when the file is merged, the file is deleted again, and the file is deleted before merging, instead of deleting the file after merging.
Java. Io. filenotfoundexception: D:/multidowntest/tmp0/setup.exe. part27.tp (the specified path cannot be found .)
At java. Io. fileoutputstream. Open (native method)
At java. Io. fileoutputstream. <init> (fileoutputstream. Java: 179)
At java. Io. fileoutputstream. <init> (fileoutputstream. Java: 70)
At com. downfile. getfilethread. Run (getfilethread. Java: 148)
At java. Lang. thread. Run (thread. Java: 595)
Solution: the original thread is used to check whether there are. TP temporary files in the Temporary Folder. If not, merge and delete the files,
Assume that there is only one temporary file at the moment, and it is exactly done at this time, so there is no temporary file below.
I executed the merge and delete operation, and my program will delete the files that are not generated at all, so an error will be reported,
Solution: The following "Why can't the file be found"

2. During the running of the program, go to the generated Temporary Folder for viewing. If 30 threads are required to be generated, but under the Temporary Folder
Only about 10 temporary files can be seen, indicating that the thread has not finished generating
There are two reasons:
1) The problem with the program itself is that the thread is not throttled, but the next operation is executed, and there is no urgent need to generate a number of threads,
Other threads are being executed.
Solution: "Why can't the file be found"

Why can't I find the file:
I think this may be the case, because when a thread is created, it is possible that only one thread is created and it is stolen by other threads,
Check whether there is a. TP file under the current folder and check it when the file is downloaded. Then it considers that there is no. TP file,
Solution:
1): Set the detection time to a little longer.
2): it must be 20 files (for example, 20 threads ).
3): the two are used at the same time. Make sure that the number of files in the current folder is the number of threads we have set to check whether it has been completed.
The detection time may not be so long, because it will delay the time, which will indirectly generate a timeout error.

3. java.net. connectexception: Connection timed out: connect, connection timeout error. If a thread
The thread reports an operation timeout error when the default wait time is exceeded because
There are many times. In order to minimize the possibility of timeout, I set the timeout time to 15 seconds through the program. Of course, it cannot be completely avoided.
The timeout error can only be avoided as much as possible, which is also related to the configuration of the personal computer and the bandwidth of the network.

4. digit formatting error:
Java. Lang. numberformatexception: NULL
At java. Lang. Long. parselong (long. Java: 372)
At java. Lang. Long. parselong (long. Java: 461)
At com. downfile. multithreadgetfile. INIT (multithreadgetfile. Java: 148)
At com. downfile. multithreadgetfile. Run (multithreadgetfile. Java: 196)
At java. Lang. thread. Run (thread. Java: 595)
Cause: an error occurs when the length of the formatted file is long and the current network may be congested at the moment and the file length cannot be obtained,
An error occurs. I didn't handle it in this aspect. Now I have been reading the file until a certain number of times.
The probability that the file length cannot be read is greatly reduced and the program Health is enhanced.

Note: Connection timeout may still occur when the program is running. If this happens, try to call the program again. At this time, the program will automatically resume resumable data transfer, the premise is that the number of download threads and file storage location are not changed.

====================================

Please do not ask me for the source code. It was written N years ago. I don't know where the source code went, and it is also my learning work. The code is messy, sorry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.