zoukankan      html  css  js  c++  java
  • 公司禁用用迅雷,禁用所有P2P协议的软件,自己写个断点续传的工具

    目前仅支持HTTP协议

    下一步的工作是支持FTP协议

    再下一步的工作是对原始URL进行智能分析

    发此文以抗议像吉日兄这样的总监或者老板

    Code
        public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();            
            }
            
    private void DownLoad()
            {
                XmlDocument xmlDoc 
    = new XmlDocument();
                xmlDoc.Load(
    "D:\\中转\\data.xml");
                XmlNodeList nodeList 
    = xmlDoc.SelectSingleNode("data").ChildNodes;
                XmlElement xe 
    = (XmlElement)nodeList.Item(nodeList.Count - 1);
                
    if (xe.GetAttribute("is_finish").Equals("true"))
                {
                    MessageBox.Show(
    "none task in the list");
                    
    return;
                }
                
    string StrFileName = "D:\\中转\\" + xe.GetAttribute("name"); 
                
    string StrUrl = xe.GetAttribute("address");

                
    long lStartPos = 0;
                System.IO.FileStream fs;
                
    if (System.IO.File.Exists(StrFileName))
                {
                    fs 
    = System.IO.File.OpenWrite(StrFileName);
                    lStartPos 
    = fs.Length;
                    fs.Seek(lStartPos, System.IO.SeekOrigin.Current); 
    //移动文件流中的当前指针
                }
                
    else
                {
                    fs 
    = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
                    lStartPos 
    = 0;
                }
                
    //打开网络连接
                try
                {
                    System.Net.HttpWebRequest request 
    = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
                    
    if (lStartPos > 0)
                        request.AddRange((
    int)lStartPos); //设置Range值
                    
    //向服务器请求,获得服务器回应数据流
                    System.IO.Stream ns = request.GetResponse().GetResponseStream();
                    
    byte[] nbytes = new byte[512];
                    
    int nReadSize = 0;
                    nReadSize 
    = ns.Read(nbytes, 0512);                
                    
    while (nReadSize > 0)
                    {
                        fs.Write(nbytes, 
    0, nReadSize);
                        nReadSize 
    = ns.Read(nbytes, 0512);
                    }
                    fs.Close();
                    ns.Close();
                    EditNode();
                    MessageBox.Show(
    "Down Ok!");
                }
                
    catch (Exception ex)
                {
                    fs.Close();
                    MessageBox.Show(
    "Down Error!");
                }
            }
            
    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
            {
                XmlDocument xmlDoc 
    = new XmlDocument();
                xmlDoc.Load(
    "D:\\中转\\data.xml");
                XmlNodeList nodeList 
    = xmlDoc.SelectSingleNode("data").ChildNodes;
                XmlElement xe 
    = (XmlElement)nodeList.Item(nodeList.Count - 1);
                
    string StrFileName = "D:\\中转\\" + xe.GetAttribute("name");
                FileInfo fi 
    = new FileInfo(StrFileName);
                DialogResult dr 
    = MessageBox.Show(thisstring.Format("already:{0}kb\nOK:exit\ncancel:continue", fi.Length / 1024), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                
    if (!dr.Equals(DialogResult.Cancel))
                {
                    
    this.Close();
                    
    this.Dispose();
                }
            }

            
    private void button1_Click(object sender, EventArgs e)
            {
                
    this.Hide();
                
    if (!string.IsNullOrEmpty(textBox1.Text.Trim())&&!string.IsNullOrEmpty(textBox2.Text.Trim()))
                {
                    InsertNode();
                }
                System.Threading.Thread obj 
    = new System.Threading.Thread(new System.Threading.ThreadStart(this.DownLoad));
                
    //设置为前台线程,即使主方法执行结束了我的线程仍在执行
                obj.IsBackground = true;
                obj.Start();
            }
            
    private void EditNode()
            {
                XmlDocument xmlDoc 
    = new XmlDocument();
                xmlDoc.Load(
    "D:\\中转\\data.xml");
                XmlNodeList nodeList 
    = xmlDoc.SelectSingleNode("data").ChildNodes;
                XmlElement xe 
    = (XmlElement)nodeList.Item(nodeList.Count - 1);
                xe.SetAttribute(
    "is_finish""true");
                xmlDoc.Save(
    "D:\\中转\\data.xml");//保存。
            }
            
    private void InsertNode()
            {
                XmlDocument xmlDoc 
    = new XmlDocument();
                xmlDoc.Load(
    "D:\\中转\\data.xml");
                XmlNode root 
    = xmlDoc.SelectSingleNode("data");
                XmlElement xe1 
    = xmlDoc.CreateElement("file");
                xe1.SetAttribute(
    "name", textBox1.Text.Trim());
                xe1.SetAttribute(
    "address", textBox2.Text.Trim());
                xe1.SetAttribute(
    "is_finish""false");
                root.AppendChild(xe1);
                xmlDoc.Save(
    "D:\\中转\\data.xml");
            }
  • 相关阅读:
    pycharm在401跑程序需要每个py文件加一句
    youtube下载视频方法
    服务器重启登陆界面死循环
    matlab2012b_win_install
    ubuntu_matlab2012b_install
    cuda8.0 + cudnn6 + tensorflow1.4 xing
    [BAT] cmd 管理员权限 右键菜单 运行
    Windows下遍历所有GIT目录更新项目脚本
    获取Xshell Xftp等官网下载地址
    Win10 企业版 激活 批处理
  • 原文地址:https://www.cnblogs.com/liulun/p/1590853.html
Copyright © 2011-2022 走看看