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

📄 downfile.java

📁 用多线程下载URL定位的文件,主要供学习java多线程的人和HTTP协议的人参考
💻 JAVA
字号:

import java.io.*;
import java.net.*;
import javax.swing.*;
/*
 * DownFile.java
 *
 * Created on 2006年12月11日, 下午5:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
/*******************************************************************************************/
/*类名:DownFile
 *功能:获取文件大小,并把文件数据分段,创建下载线程
 *方法:
 *     构造方法 DownFile(String localFilePath,String stringURL)
 *     获取文件大小 void getFileSize()
 *     线程方法void run()
 */
/*******************************************************************************************/
public class DownFile extends Thread{
    String localFilePath;
    String stringURL;
    DownFileSplitter[] downFileSplitter;
    boolean[] bool;
    //提示框信息
    JTextArea messageJTextArea;
    //文件长度
    int fileLength;
    //下载线程数
    int nThread;
    //下载起始位置
    int[] startPos;
    //下载结束位置
    int[] endPos;
    /** Creates a new instance of DownFile */
    public DownFile(String localFilePath,String stringURL,JTextArea messageJTextArea){
        this.localFilePath=localFilePath;
        this.stringURL=stringURL;
        bool=new boolean[5];
        this.messageJTextArea=messageJTextArea;
        //下载线程数
        nThread=5;
        startPos=new int[nThread];
        endPos=new int[nThread];
        downFileSplitter=new DownFileSplitter[nThread];
        fileLength=-1;
    }
    //获取文件大小
    public int getFileSize(){
        URL url;
        HttpURLConnection hURL;
        int fLength=-1;
        try {
            url=new URL(stringURL);
            hURL=(HttpURLConnection) url.openConnection();
            int code=hURL.getResponseCode();
            if(code==-1){
                //System.out.println("不是有效的状态码!");
                messageJTextArea.append("\n不是有效的状态码!");
                hURL.disconnect();
                return -1;
            }
            if(code==400){
                //System.out.println("发生错误!");
                messageJTextArea.append("\n网络出现问题!无法连接!");
                hURL.disconnect();
                return -1;
            }
            if(code==200){
                //System.out.println("正确接收信息!");
                messageJTextArea.append("\n连接成功,准备下载!");
            String sHeader;
            for(int i=1;;i++){
                //查找标识文件长度的文件头,获取文件长度
                sHeader=hURL.getHeaderFieldKey(i);
                if(sHeader!=null){
                    if(sHeader.equals("Content-Length")){
                        fLength=
                                Integer.parseInt(hURL.getHeaderField(sHeader));
                        break;
                    }
                } else
                    break;
            }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        //System.out.println("文件大小:"+fLength+" byte");
        messageJTextArea.append("\n所下载文件的大小:"+fLength/1024 +" K");        
        return fLength;
    
    }
    //分段
    public boolean subsectionDown(){
        if((fileLength=getFileSize())!=-1){
            int subLength=fileLength/nThread;
            for(int i=0;i<nThread;i++){
                if(i==0){
                    startPos[i]=0;
                } else
                    startPos[i]=endPos[i-1];
                if(i!=nThread-1){
                    endPos[i]=startPos[i]+subLength;
                } else
                    endPos[i]=fileLength;
                //System.out.println(i+"段的开始:"+startPos[i]);
                //System.out.println(i+"段的结束:"+endPos[i]);
            }
            return true;
        } else {
            //System.out.println("下载失败!");            
            messageJTextArea.append("获取不到文件的大小,下载失败!");
            return false;
        }
    }
    //线程函数
    public void run(){
        //分段
        if(this.subsectionDown()){
        //创建nThread个线程,用于下载
        for(int i=0;i<nThread;i++){
            messageJTextArea.append("\n下载子线程"+(i+1)+"启动......");
            downFileSplitter[i]=new DownFileSplitter
                    (localFilePath,stringURL,startPos[i],endPos[i],messageJTextArea,i+1);
            downFileSplitter[i].start();
            //判断子线程是否下载完毕
        }        
        //是否结束while循环
        boolean breakWhile = false;  
        
        while(true) {
            int n=0;
            for(int i=0;i<nThread;i++) {
                if(downFileSplitter[i].bDownOver&&!downFileSplitter[i].isAlive()) {
                    n=n+1;                    
                }
            }
            if(n==nThread){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
                    messageJTextArea.append("\n整个文件下载完毕!");
                    messageJTextArea.requestFocus();     
                    messageJTextArea.selectAll();
                    messageJTextArea.transferFocus();
                    break;
                }
                }
        //messageJTextArea.append("\n已经完成整个文件的下载!");      
    }
    //供此类测试用
//    public static void main(String[] args) {
//        DownFile file=new DownFile("H:\\2.exe",
//                "http://www.zhouyunhai.com/book/whomovedmycheese.exe");
//        file.start();
//    }
    else
        messageJTextArea.append("\n下载失败!");
    }
}

⌨️ 快捷键说明

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