2017-01-12 7 views
0

からコンテンツ長を取得:HttpURLConnectionのは、私は私のダウンロードを再開するために、次の接続を使用してサーバ

connection = (HttpURLConnection) url.openConnection(); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setReadTimeout(7000); 
connection.setRequestProperty("Range", "bytes=" + localFileSize + "-"); 

long fileLengthOnServer = connection.getContentLength(); 
connection.connect(); 

fileLengthOnServerの値が-1です。誰でもどんな条件でfileLengthOnServer-1になるか説明できますか?私は、localFileSizeがサーバ上のものと同じであるとは確信していません。助けてください!

答えて

2

connection.connect()の後にlong fileLengthOnServer = connection.getContentLength();とする必要があります。最初にサーバに接続しなくても応答ヘッダを取得することはできません

+0

openConnectionメソッドを呼び出すことでconnection.connect()を呼び出す必要はありません。すでに接続状態にあり、接続コールは無視されます。 connect()メソッドのドキュメントの通りです。応答ストリームに問題がある可能性があります。 – PyThon

関連する問題