java 使用 HTTP 请求下载文件的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17643851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:31:05  来源:igfitidea点击:

Downloading a portion of a File using HTTP Requests

javaurlconnectiondownloaddownloading-website-files

提问by Jeffry Young

I am trying to download a portion of a PDF file (just for testing "Range" header). I requested the server for the bytes (0-24) in Range but still, instead of getting first 25 bytes (a portion) out of the content, I am getting the full length content. Moreover, instead of getting response code as 206 (partial content), I'm getting response code as 200.

我正在尝试下载 PDF 文件的一部分(仅用于测试“范围”标题)。我请求服务器获取 Range 中的字节 (0-24),但仍然没有从内容中获取前 25 个字节(一部分),而是获取了全长内容。此外,我得到的响应代码不是 206(部分内容),而是 200。

Here's my code:

这是我的代码:

public static void main(String a[]) {
    try {
        URL url = new URL("http://download.oracle.com/otn-pub/java/jdk/7u21-b11/jdk-7u21-windows-x64.exe?AuthParam=1372502269_599691fc0025a1f2da7723b644f44ece");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Range", "Bytes=0-24");
        urlConnection.connect();

        System.out.println("Respnse Code: " + urlConnection.getResponseCode());
        System.out.println("Content-Length: " + urlConnection.getContentLengthLong());

        InputStream inputStream = urlConnection.getInputStream();
        long size = 0;

        while(inputStream.read() != -1 )
            size++;

        System.out.println("Downloaded Size: " + size);

    }catch(MalformedURLException mue) {
        mue.printStackTrace();
    }catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

Here's the output:
Respnse Code: 200
Content-Length: 94973848
Downloaded Size: 94973848

这是输出:
Respnse Code: 200
Content-Length: 94973848
Downloaded Size: 94973848

Thanks in Advance.

提前致谢。

回答by Syed Muhammad Humayun

Try changing following:

尝试更改以下内容:

urlConnection.setRequestProperty("Range", "Bytes=0-24");

with:

和:

urlConnection.setRequestProperty("Range", "bytes=0-24");

as per the spec 14.35.1 Byte Ranges

根据规范14.35.1 字节范围

Similarly, as per the spec 14.5 Accept-Ranges, you can also check whether your server actually supports partial content retrieval or not using following:

同样,根据规范14.5 Accept-Ranges,您还可以使用以下方法检查您的服务器是否实际支持部分内容检索:

boolean support = urlConnection.getHeaderField("Accept-Ranges").equals("bytes");
System.out.println("Partial content retrieval support = " + (support ? "Yes" : "No));

回答by Jeffry Young

If the server supports it (and HTTP 1.1 servers should), only then you can use range requests... and if all you want to do is check, then just send a HEAD request instead of a GET request. Same headers, same everything, just "HEAD" instead of "GET". If you receive a 206 response, you'll know Range is supported, and otherwise you'll get a 200 response.

如果服务器支持它(并且 HTTP 1.1 服务器应该),只有这样你才能使用范围请求......如果你想做的只是检查,那么只需发送 HEAD 请求而不是 GET 请求。相同的标题,相同的一切,只是“HEAD”而不是“GET”。如果您收到 206 响应,您就会知道 Range 受支持,否则您将收到 200 响应。

回答by M.Barati

You have to connect to url beforesetRequestProperty

您必须setRequestProperty之前连接到 url

Change:

改变:

urlConnection.setRequestProperty("Range", "Bytes=0-24");
urlConnection.connect();

To:

到:

urlConnection.connect();
urlConnection.setRequestProperty("Range", "Bytes=0-24");

回答by M.Barati

I think the correct header is "Content-Range", not "Range" as you are using.

我认为正确的标题是“内容范围”,而不是您使用的“范围”。