首页 > Java > java教程 > 正文

JAVA下载中getContentLength()一直为-1的一些思路

巴扎黑
发布: 2017-06-26 10:21:24
原创
1815 人浏览过
如果Content Length 在头文件中没有描述

暂时还没有解决方案

如果Content Long在头文件中有描述

 


方案一:

 伪装成浏览器

 conn.setRequestProperty("User-Agent", " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
登录后复制

代码中加入代理

String host = "127.0.0.1";    
     String port = "8888";    
     setProxy(host, port);   
     public static void setProxy(String host, String port) {    
    System.setProperty("proxySet", "true");    
    System.setProperty("proxyHost", host);    
    System.setProperty("proxyPort", port);    
}
登录后复制

方案二:

加入以下属性,让服务器不要gzip方式压缩:

Java Doc 有对此的描述:

By default, this implementation of HttpURLConnection requests that servers use gzip compression. Since getContentLength() returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream(). Instead, read that stream until it is exhausted: whenread() returns -1.

conn.setRequestProperty("Accept-Encoding", "identity");
登录后复制

 

以上是JAVA下载中getContentLength()一直为-1的一些思路的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!