日期:2014-05-20  浏览次数:20967 次

各位前辈麻烦你们帮我解决下这个问题,是关于HttpURLConnection类的问题?

传智播客视频里的一段代码
Java code
package cn.zcp.downlod;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class DownlodMessage
{
    public static void main(String[] args) throws Exception
    {
        Scanner can = new Scanner(System.in);
        String path = can.next();
        new DownlodMessage().dowload(path,4);
    }

    private void dowload(String path, int threadsize) throws Exception
    {
        URL url = new URL(path);
        HttpURLConnection conn =(HttpURLConnection) url.openConnection();
        
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() ==200)
        {
            int len = conn.getContentLength();
            File file = new File(this.getFilename(path));
            RandomAccessFile ran = new RandomAccessFile(file, "rwd");
            ran.setLength(len);
            ran.close();
        
            int block = len%threadsize ==0 ? len/threadsize:len/threadsize+1; 
            for(int i = 0 ; i< threadsize; i++)
            {
                new MyThread(threadsize,file,url,block).start();
            }
        }
        else
        {
            System.out.println("下载失败");
        }
    }
    
    private class MyThread extends Thread
    {
        private int threadid;
        private File file;
        private URL url;
        private int block;

        public MyThread(int threadsize, File file, URL url, int block)
        {
            this.block = block;
            this.file = file;
            this.threadid = threadsize;
            this.url = url;
        }

        @Override
        public void run()
        {
            int start = threadid*block;
            int end = (threadid+1)*block - 1;
            
            try
            {
                RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");
                accessFile.seek(start);
                HttpURLConnection http = (HttpURLConnection)url.openConnection();
                http.setConnectTimeout(5000);
                http.setRequestMethod("GET");
                http.setRequestProperty("Range", "bytes="+start+"-"+end);
                if(http.getResponseCode() == 206)
                {
                System.out.println(http.getResponseCode());
                    InputStream iStream =  http.getInputStream();
                    int lenght = 0;
                    byte[] buffer = new byte[2048];
                    while((lenght = iStream.read(buffer , 0, buffer.length))!=-1)
                    {
                        accessFile.write(buffer, 0, lenght);
                    }
                    accessFile.close();
                    iStream.close();
                    System.out.println("第"+(threadid+1)+"个线程下载完成");
                }
                else
                {
                    System.out.println("下载失败");
                }
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        
    }

    private String getFilename(String path)
    {
        String name = path.substring(path.lastIndexOf("/")+1);
        
        return name;
    }
    
    

}


http://t2.baidu.com/it/u=1830717818,1080604890&fm=24&gp=0.jpg
http://t2.baidu.com/it/u=1830717818,1080604890&fm=24&gp=0.jpg
Exception in thread "main" java.net.ProtocolException: Can't reset method: already connected