>  기사  >  Java  >  JAVA 다운로드에서 getContentLength()가 항상 -1인 이유에 대한 몇 가지 아이디어

JAVA 다운로드에서 getContentLength()가 항상 -1인 이유에 대한 몇 가지 아이디어

巴扎黑
巴扎黑원래의
2017-06-26 10:21:241804검색
헤더 파일에 Content Length가 기술되어 있지 않은 경우

아직 해결 방법이 없습니다

Content Long이 헤더 파일에 기술되어 있는 경우


옵션 1:

브라우저로 위장

 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);    
}

옵션 2:

다음 속성 추가 서버가 gzip으로 압축되는 것을 방지하기 위해 :

Java Doc에는 이에 대한 설명이 있습니다.

기본적으로 이 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() 구현은 -1을 반환합니다.

conn.setRequestProperty("Accept-Encoding", "identity");

위 내용은 JAVA 다운로드에서 getContentLength()가 항상 -1인 이유에 대한 몇 가지 아이디어의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.