Today I'm going to write a multithreaded breakpoint download program in the form of a generic jar file, but to consider the user has downloaded no download to complete the preservation of the file saved, think of several ways, one is a text file, consider reading and writing up more trouble, want to read in a row of all the analysis, give up; the second is to use the database , but considering that not all users like it, and the database also occupies a larger space, give up; the third way is to use the XML, think of this way of the transplant is strong, many platforms are supported, and read and write up is also more convenient, so decided to use this way, the following is today plus a little experience , including the creation of XML files, find, add, delete, save and other operations, first put here, and later useful, then to take, note, to run the following program, please download Jdom first, and the environment variable:
import java.io.File;
import Java.io.FileOutputStream;
import java.util.List;
import Org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import Org.jdom.input.SAXBuilder;
import Org.jdom.output.XMLOutputter;
import Org.jdom.xpath.XPath;
/**
* Create an XML file dynamically in this class to save the downloaded file name and path
* So that the breakpoint renewal function of these downloads can be used
* The XML file format created is as follows:
* <?xml version= "1.0" encoding= "UTF-8"?>
<downFile>
<file internetpath= "Http://www.123.com/xx.rar" >
<localPath>c://tmp</localPath>
</File>
<file internetpath= "Http://www.123.com/xx2.rar" >
<localPath>c://tmp0</localPath>
</File>
</downFile>
*/
Public class Filedowninformationwithxml {
Saxbuilder Builder;
Document document;
Element Root;
String xmlfilename = "Downfile.xml";
String Fileandpath; Full path to XML document
/**
*
* @param path for downloading temporary files
*/
Public Filedowninformationwithxml (String Path) {
Builder = new saxbuilder ();
this. Fileandpath = Path + xmlfilename;
if (checkxmlfileexist () = false) {
if (Createxmlfile () = false) {
SYSTEM.OUT.PRINTLN ("XML file creation error, make sure you have permission");
return;
}
}
Try {
Document = Builder.build (Fileandpath);
Catch (Exception e) {
E.printstacktrace ();
}
root = Document.getrootelement ();
System.out.println ("Root:" +root.getname ());
}
/**
* Returns the local address obtained based on the network address, which is used at the breakpoint continuation
* If the null value is returned, then it means that there is no download list and should be downloaded again
*/