使用HTTP读取文件的第一部分 [英] Reading the first part of a file using HTTP

查看:110
本文介绍了使用HTTP读取文件的第一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过阅读文件的第一部分并分析内容来确定文件的类型(通常为UTF-8)。 (该类型特定于我的社区,但不在我的控制之下,并且不包含在MIME / MediaType中,通常是TEXT_PLAIN)。我正在使用客户端上的'org.restlet'库来分析标题

I would like to determine the type of a file (generally UTF-8) by reading the first part of the file and analysing the content. (The type is specific to my community but not under my control and not covered by MIME/MediaType which is normally TEXT_PLAIN). I am using the 'org.restlet' library on the client to analyse the header with

Request request = new Request(Method.HEAD, url);

所以我知道内容长度,并且可以(如果必要和可能)估计我应该多少字节下载进行分析

so I know the content-length and can (if necessary and possible) estimate how many bytes I should download for the analysis

澄清:我无法使用MediaType。从答案1看起来我必须得到内容。因此修改后的问题是:

CLARIFICATION: I cannot use the MediaType. From answer 1 seems like I have to GET the content. A revised question would therefore be:

我可以使用Restlet获取文件的部分吗?

"Can I GET part of a file using Restlet?"

答案:
以下代码可以满足我的需求。我已经将@BalusC归功于展示方式。如果我遗漏了任何内容,请评论:

ANSWER: The following code does what I want. I have credited @BalusC for showing the way. Please comment if I have missed anything:

public String readFirstChunk(String urlString, int byteCount) {
    String text = null;
    if (urlString != null) {
        org.restlet.Client restletClient = new org.restlet.Client(Protocol.HTTP);
        Request request = new Request(Method.GET, urlString);
        List<Range> ranges = Collections.singletonList(new Range(0, byteCount));
        request.setRanges(ranges);
        Response response = restletClient.handle(request);
        if (Status.SUCCESS_OK.equals(response.getStatus())) {
            text = processSuccessfulChunkRequest(response);
        } else if (Status.SUCCESS_PARTIAL_CONTENT .equals(response.getStatus())) {
            text = processSuccessfulChunkRequest(response);
        } else {
            System.err.println("FAILED "+response.getStatus());
        }
    }
    return text;
}

private String processSuccessfulChunkRequest(Response response) {
    String text = null;
    try {
        text = response.getEntity().getText();
    } catch (IOException e) {
        throw new RuntimeException("Cannot download chunk", e);
    }
    return text;
}


推荐答案

这是唯一可能的服务器发送了 Accept-Ranges Content-Range 标题以及 ETag 上次修改 。例如

That's only possible if the server has sent the Accept-Ranges and Content-Range headers along with ETag or Last-Modified. E.g.

Accept-Ranges: bytes
Content-Range: bytes 0-1233/1234
ETag: file.ext_1234_1234567890

Accept-Ranges:bytes 表示服务器支持在指定字节范围内返回部分内容的请求。 Content-Range 标题通知长度。 ETag Last-Modified 表示请求URI后面的资源上的唯一文件标识符或上次修改的时间戳。

The Accept-Ranges: bytes indicates that the server supports requests returning partial content in a specified byte range. The Content-Range header informs about the length. The ETag and Last-Modified indicate the unique file idenfier or the last modified timestamp on the resource behind the request URI.

如果响应中存在这些标题,那么您可以使用 If-Range 范围 请求标题分别包含唯一文件标识符或上次修改的时间戳和所需的字节范围。

If those headers are present in the response, then you can request a part of the resource using If-Range and Range request headers with respectively the unique file identifier or the last modified timestamp and the desired byte range.

If-Range: file.ext_1234_1234567890
Range: bytes=0-99

上面的例子返回文件的前100个字节。

The above example returns the first 100 bytes of the file.

这篇关于使用HTTP读取文件的第一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆