⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedowninformationwithxml.java

📁 用java编制的断点续传的源程序
💻 JAVA
字号:
package com.downfile;

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;

/**
 * 在该类中将动态的创建XML文件用以保存下载的文件名及路径
 * 以便这些下载文件的断点续传功能可以使用
 * 创建的XML文件格式如下:
 * <?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; //XML文档的完整路径


    /**
     * 
     * @param Path 存放下载临时文件的路径
     */
    public FileDownInformationWithXML(String Path) {
        builder = new SAXBuilder();
        this.fileAndPath = Path + XMLFileName;
        if (checkXMLFileExist() == false) {
            if (createXMLFile() == false) {
                System.out.println("XML文件创建错误,请确认您是否有权限");
                return;
            }
        }

        try {
            document = builder.build(fileAndPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        root = document.getRootElement();
        //System.out.println("root:"+root.getName());
    }

    /**
     * 返回根据网络地址得到的本地地址,这是在断点续传中使用
     * 如果返null值,那么就说明该没有不存在下载列表中,就应该重新下载
     */
    public String downFileExist(String internetAddress) {
        String localPath = null;
        XPath xPath; //查找id=1的是不是存在
        try {
            //下面是查找文件名及下载地址都相同的项
            //xPath = XPath.newInstance("/downFile/File[@name='"+file+"']/internetPath[text()='"+internetAddress+"']");
            xPath = 
                    XPath.newInstance("//File[@internetPath='" + internetAddress + 
                                      "']");
            List list = xPath.selectNodes(document);
            if (list.size() == 1) {
                Element e = (Element)list.get(0);
                localPath = e.getChildText("localPath");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localPath;
    }

    /**
     * 根据网络地址及本地地址,增加一个节点
     */
    public void addOneDownRecord(String internetAddress, String localAddress) {
        Element e = new Element("File");
        root.addContent(e);
        Attribute attribute = new Attribute("internetPath", internetAddress);
        e.setAttribute(attribute);
        Element e1 = new Element("localPath");
        e1.setText(localAddress);
        e.addContent(e1);
    }

    /**
     * 根据网络地址删除一个节点
     */
    public boolean removeOneDownRecord(String internetAddress) {
        XPath xpath;
        try {
            xpath = 
                    XPath.newInstance("//File[@internetPath='" + internetAddress + 
                                      "']");
            List list = xpath.selectNodes(root);
            if (list.size() == 1) {
                Element e = (Element)list.get(0);
                root.removeContent(e);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 将更改的结果保存到磁盘
     * @return
     */
    public boolean saveChange() {
        XMLOutputter out;
        try {
            out = new XMLOutputter();
            out.output(root, new FileOutputStream(fileAndPath));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean saveChange(Document doc) {
        XMLOutputter out;
        try {
            out = new XMLOutputter();
            out.output(doc, new FileOutputStream(fileAndPath));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 检查当前XML是否存在
     * @return
     */
    public boolean checkXMLFileExist() {
        File file = new File(fileAndPath);
        if (file.exists())
            return true;
        else
            return false;
    }

    /**
     * 在XML文件不存在的情况,那么就要建立一个XML文件
     */
    public
    //注:这里不能够用newFile的方法来创建,必须指定一个根,否则要出错
    //最好采用下面的方式创建
    boolean createXMLFile() {
        try {
            Element e = new Element("downFile");
            Document doc = new Document(e);
            saveChange(doc); //保存创建的文件
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 确定XML文件是否没有下载项了,此时等下载完毕就可以把该文件给删除掉
     * @return
     */
    public boolean checkXMLIsEmpey() {
        List listChild = root.getChildren();      
        if (listChild.size() == 0)
            return true;
        return false;
    }
    /**
     * 删除XML文件,(可是这里却删除不掉,有可能是程序没有退出,那就表示此时该文件一直被使用)
     */
    public void deleteXMLFile() {
        File file2=new File(fileAndPath);
        file2.delete();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -