package com.junjun.download;import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;public class MulThreadDownload { public static void main(String[] args) throws Exception{
String path = "http://192.168.1.101:8080/videonews/thinkpython.pdf";
new MulThreadDownload().download(path,3);
}
/**
 * 下载文件
 * @param path 文件路径
 */
private void download(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 length = conn.getContentLength();//获得网络文件的长度
File file = new File(getFilename(path));
RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");//在本地生成与网络文件长度相等的文件accessFile
accessFile.setLength(length);
accessFile.close();
//计算每条线程负责下载的数据量
int block = length%threadsize==0 ? length/threadsize : length/threadsize + 1;
for(int threadid = 0 ; threadid<threadsize ; threadid++){
new DownloadThread(threadid,block,url,file).start();
}
}else{
System.out.println("访问失败!");
}

}
private class DownloadThread extends Thread{
private int threadid;
private int block;
private URL url;
private File file;
public DownloadThread(int threadid, int block, URL url, File file) {
this.block = block;
this.file = file;
this.threadid = threadid;
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
accessFile.seek(start);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes =" + start + "-" + end);
//if(conn.getResponseCode()==206){
System.out.println(conn.getResponseCode());
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = inStream.read(buffer)) != -1){
accessFile.write(buffer, 0, len);
}
accessFile.close();
inStream.close();
//}
System.out.println("第"+ (threadid+ 1)+"该线程下载完成");
} catch (Exception e) {
e.printStackTrace();
}

}

}
private String getFilename(String path) {

return path.substring(path.lastIndexOf("/")+1);
}
}出现如下的错误,如果将第62和72行的注解去掉的话,下载得到的文件会受损。哪位大神帮小弟解决这个问题吧,小弟初学者。416
416
java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:64)
Caused by: java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:63)
java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:64)
Caused by: java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:63)
416
java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:64)
Caused by: java.io.IOException: Server returned HTTP response code: 416 for URL: http://192.168.1.101:8080/videonews/thinkpython.pdf
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.junjun.download.MulThreadDownload$DownloadThread.run(MulThreadDownload.java:63)多线程URL

解决方案 »

  1.   

    java.io.IOException: Server returned HTTP response code: 416 解释如下:
    416 Requested Range Not Satisfiable
    如果请求中包含了Range请求头,并且Range中指定的任何数据范围都与当前资源的可用范围不重合,同时请求中又没有定义If-Range请求头,那么服务器就应当返回416状态码。
    假如Range使用的是字节范围,那么这种情况就是指请求指定的所有数据范围的首字节位置都超过了当前资源的长度。服务器也应当在返回416状态码的同时,包含一个Content-Range实体头,用以指明当前资源的长度。这个响应也被禁止使用multipart/byteranges作为其Content-Type。
    所以认真检查下你给出的范围参数信息吧。
      

  2.   

    解决了,原来是这行代码的问题,conn.setRequestProperty("Range", "bytes =" + start + "-" + end);估计是输入法的问题,晕死了,重新写了下就好了,还是谢谢你