import java.io.*;
import java.net.URL;
import java.net.URLConnection;public class DownLoadMultiThread {
    public static void main(String[] args) {
        try {
            //初始化开始
            System.out.println("请正确输入URL地址 :");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String urlstr = bufferedReader.readLine().trim();
            URL url = new URL(urlstr);
            URLConnection uc = url.openConnection();
            int length = uc.getContentLength();
            String fileName = getFileName(url.getFile());
            System.out.println("文件名 :"+fileName);
            System.out.println("文件长度 :"+length);
            File file = new File(fileName);
            file.createNewFile();
            //初始化结束
            //----------------------------------------------------------------------------------
            //初始化线程
            int threadCount=10;//线程数目
            long size=length/threadCount;//每个线程需要下载的大小
            if(size<1){
                threadCount=1;
            }
            Thread[] thread=new Thread[threadCount];
            int i=0;
            for(i=0;i<threadCount-1;i++){
                long position=i*size+1;
                thread[i]=new DownThread(file,position,size,getStream(urlstr));
                System.out.println("创建第"+(i+1)+"个线程,启始位置"+position+",文件块长度"+size);
            } //初始化前9个线程
            long lastPosition=i*size+1;
            long lastSize=length-i*size;
            thread[i]=new DownThread(file,lastPosition,lastSize,getStream(urlstr));//初始化地10个线程
            System.out.println("创建第10个线程,启始位置"+lastPosition+",文件块长度"+lastSize);
            //----------------------------------------------------------------------------------
            for(int j=0;j<threadCount;j++){
                thread[j].start();
                System.out.println("启动第"+j+"个线程");
            }//启动
            RandomAccessFile raf=new RandomAccessFile(file,"r");
            for(int j=0;j<threadCount;j++){
                byte[] temp=((DownThread) thread[j]).getBuffer();
                //raf.write(temp,(int)((DownThread) thread[j]).getPosition(),(int)((DownThread) thread[j]).getLength());
                raf.write(temp);
                thread[j].join();
            }
            raf.close();
        }catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    private static String getFileName(String str){
        String[] strs= str.split("/");
        return strs[strs.length-1];
    }
    private static URLConnection getStream(String url){
        URLConnection uc = null;
        try {
            URL urls = new URL(url);
            uc = urls.openConnection();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return uc;
    }
}
class DownThread extends Thread{
    private File file;    public long getPosition() {
        return position;
    }    public long getLength() {
        return length;
    }    private long position;
private long length;
private byte[] buffer;
    private URLConnection uc;
    private InputStream is;
    public DownThread(File file,long position,long length,URLConnection uc){
        try {
            this.file=file;
            this.position=position;
            this.length=length;
            this.uc = uc;
            this.is = uc.getInputStream();
            buffer=new byte[(int)length];
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
public void run(){
        try {
            int len;
            while ((len = is.read(buffer)) > 0) {
                 //
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    public byte[] getBuffer(){
return buffer;
}
}

解决方案 »

  1.   

    以下是打印出来的问题请正确输入URL地址 :
    http://61.128.162.178/filenew08/10341/4.wma
    文件名 :4.wma
    文件长度 :2360323
    创建第0个线程,启始位置1,文件块长度236032
    创建第1个线程,启始位置236033,文件块长度236032
    创建第2个线程,启始位置472065,文件块长度236032
    创建第3个线程,启始位置708097,文件块长度236032
    创建第4个线程,启始位置944129,文件块长度236032
    创建第5个线程,启始位置1180161,文件块长度236032
    创建第6个线程,启始位置1416193,文件块长度236032
    创建第7个线程,启始位置1652225,文件块长度236032
    创建第8个线程,启始位置1888257,文件块长度236032
    创建第10个线程,启始位置2124289,文件块长度236035
    启动第0个线程
    启动第1个线程
    启动第2个线程
    启动第3个线程
    java.io.IOException: 拒绝访问。
    启动第4个线程
    启动第5个线程
    启动第6个线程
    启动第7个线程
    启动第8个线程
    启动第9个线程
    at java.io.RandomAccessFile.writeBytes(Native Method)
    at java.io.RandomAccessFile.write(RandomAccessFile.java:435)
    at com.feihp.test1.DownLoadMultiThread.main(DownLoadMultiThread.java:59)
      

  2.   

    不用的,因为多个线程都是在写一个文件不同的位置。不用Synchronized也没问题。这样的多线程下载我做过,要的可以发mail给我。[email protected]