基于java下载中getContentLength()一直为

网友投稿 248 2023-05-07


基于java下载中getContentLength()一直为

如果Content Length 在头文件中没有描述

暂时还没有解决方案

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

方案一:

伪装成浏览器

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

代码中加入代理

String host = "127.0.0.1";

String port = "8888";

setProxy(hoseiNqgDYmbt, port);

public static void setProxy(String host, String port) {

eiNqgDYmbSystem.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");


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MyBatis中OGNL的使用教程详解
下一篇:接口的实现方式(接口实现接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~