Http 416错误解决方案
分类: Java
今天写多线程下载时老报Http416的错误,正常的话应该返回code 206,但是这次却不正常,后来研究了一下,发现如下问题:
以下是错误log:
Server returned HTTP response code: 416 for URL: http://localhost:8080/QQ.exe
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.lyc.download.Demo$DownloadThread.run(Demo.java:97)
HTTP response code: 416是由于读取文件时设置的Range有误造成的,具体的说就是下面这行代码有误:
connection.setRequestProperty("Range", "bytes=:"+startIndex+"-"+endIndex);
中间的冒号:就是罪魁祸首,敲代码的时候不小心多敲了个冒号造成的;