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

📄 filesplitdown.java

📁 Java写的下载文件程序
💻 JAVA
字号:
/* FileSplitDown.java */ 

import java.io.*; 
import java.net.*; 

public class FileSplitDown extends Thread
{
  //定义私有成员对象
  private String urlString=null;            //下载文件的网络路径字符串
  private String fileString=null;           //文件保存路径字符串
  private long startLong=0;                 //文件分片的起始位置
  private long endLong=0;                   //文件分片的结束位置 
  private int threadInt=0;                  //子线程序号
  private int priorityInt=0;                //子线程优先权
  private boolean splitdownBoolean = false; //下载线程完成标识 
  private boolean splitstopBoolean = false; //下载线程停止标识 
  private RandomAccessFile saveFile=null;   //保存下载内容的文件

  //构造函数
  public FileSplitDown(String urlString,String fileString,long start,
                   long end,int id,int priority)  throws IOException
  { 
   this.urlString = urlString; 
   this.fileString =fileString;
   startLong = start;
   endLong = end; 
   threadInt = id;
   priorityInt = priority;
   setPriority(priorityInt);
   saveFile = new RandomAccessFile(fileString,"rw");
   saveFile.seek(startLong);
  } 

  //下载子线程的主体
  public void run() 
  { 
   while((startLong < endLong) && (!splitstopBoolean))
   { 
    try
    {
     URL url = new URL(urlString); 
     HttpURLConnection httpConnection;
     httpConnection=(HttpURLConnection)url.openConnection();
     httpConnection.setRequestProperty("User-Agent","DDXC"); 
     String propertyString = "bytes="+startLong+"-"; 
     httpConnection.setRequestProperty("RANGE",propertyString); 
     InputStream input = httpConnection.getInputStream(); 
     //logResponseHead(httpConnection); 
     byte[] b = new byte[1024]; 
     int readInt; 
     while(((readInt=input.read(b,0,1024)) > 0) && (startLong<endLong)
                                               && (!splitstopBoolean))
       startLong = startLong+write(b,0,readInt); 
    } 
    catch(Exception e){ e.printStackTrace();} 
   }
   if(startLong >= endLong) 
   {
    System.out.println("Thread " + threadInt + " is over!");
    splitdownBoolean = true; 
   }
   try { saveFile.close();}
   catch(IOException ee) { ee.printStackTrace(); }
  }//结束成员函数run() 
  
  /*以下部分是公共成员函数*/

  //结束下载子线程
  public void splitDownStop()
  { 
   splitstopBoolean = true; 
  } 

  //取得当前子线程完成信息
  public boolean getSplitDown()
  {
   return splitdownBoolean;
  }

  //取得当前下载子线程的起始位置
  public long getStart()
  {
   return startLong;
  }

  //取得当前下载子线程的结束位置
  public long getEnd()
  {
   return endLong;
  }

  /*以下部分是私有成员函数*/

  //将下载的信息写入保存文件中,需要并发控制
  private synchronized int write(byte[] b,int start,int len)
  { 
   int n = -1; 
   try
   {
    saveFile.write(b,start,len);
    n = len; 
   } 
   catch(IOException e) { e.printStackTrace (); } 
   return n; 
  } 

  //打印回应的头信息 
  private void logResponseHead(HttpURLConnection con) 
  { 
   for(int i=1;;i++) 
   { 
    String header=con.getHeaderFieldKey(i); 
    if(header==null) break;
    System.out.println(header+" : "+con.getHeaderField(header)); 
   } 
  } 

}//结束类FileSplitDown

⌨️ 快捷键说明

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