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

📄 multidownload.java~1~

📁 多线程下载器。该小程序模范迅雷等下载器的下载原理实现多线程下载
💻 JAVA~1~
字号:
package com;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

public class MultiDownload {
        int i=0;
        int fileMark = 0;
        DownloadUtil downUtil = new DownloadUtil();
        boolean isStop[] = new boolean[10];


        public void downProcess(String url,int byteCount,int threadNum) throws Exception{
                while(i<threadNum){
                        final int startPosition = byteCount*i;
                        final int endPosition = byteCount*(i+1);

                        File file = new File("temp_file_"+i+".temp");
                        DownThread fileThread = new DownThread(url,file,
                                        startPosition,endPosition,this,i);
                        new Thread(fileThread).start();
                        i++;
                }
        }


        public void downMulitFile(List downList) throws Exception{
                for(int k=0;k<downList.size();k++){
                        DownloadInfo downInfo = (DownloadInfo)downList.get(i);
                        String url = downInfo.getUrl();
                        int threadNum = downInfo.getThreadNum();
                        String filename = downInfo.getFilename();
                        int fileLength  = downUtil.getFileLength(url);

                        if(fileLength!=-1){
                                final int byteCount = (int)(fileLength/threadNum)+1;
                                boolean stopFlag = true;
                                downProcess(url,byteCount,threadNum);
                                stopFlag = downUtil.isFinished(isStop);
                                if(stopFlag){
                                        File file = new File(filename);
                                        uniteFile(threadNum,file);
                                }
                        }
                }
        }


        public void uniteFile(int threadNum,File file) throws Exception{
                for(int i=0;i<threadNum;i++){
                        File tempfile = new File("temp_file_"+i+".temp");
                        FileInputStream fis = new FileInputStream(tempfile);
                        RandomAccessFile raf = new RandomAccessFile(file, "rw");
                        byte[] buf = new byte[1024];
                        int len = -1;
                        raf.seek((long) fileMark);

                        while ((len = fis.read(buf)) != -1) {
                                raf.write(buf, 0, len);
                                fileMark += len;
                        }

                        fis.close();
                        raf.close();
                        tempfile.delete();
                }
        }
}

⌨️ 快捷键说明

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