我使用以下连接来恢复下载:
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
是否与服务器上的相同。请帮忙!
最佳答案
您需要在connection.connect()
之后调用long fileLengthOnServer = connection.getContentLength();
。如果不先连接到服务器,则无法获取响应 header
关于java - HttpURLConnection 从服务器获取内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41612016/