传智播客视频里的一段代码
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
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:320)
at cn.zcp.downlod.DownlodMessage.dowload(DownlodMessage.java:28)
at cn.zcp.downlod.DownlodMessage.main(DownlodMessage.java:18)

解决方案 »

  1.   

    我对web的东西一点都不懂啊就是小白,我是想下载一张图片。从网页上下载下来。不能这样吗?
      

  2.   

    这程序没有问题,你下载http://img.baidu.com/img/image/ilogob.gif这个试试,是可以的,主要是你的url不正确
      

  3.   

    麻烦你说说为什么我那个URL不行。那我如果要实现网络上的一些ZIP文件的下载,需要什么样的URL
    我网页这块是小白,所以麻烦你了。
      

  4.   

    那个URL是禁止访问的,就是无法下载,所以会不成功