使用HTTP读取文件的第一部分

编程入门 行业动态 更新时间:2024-10-09 02:23:01
本文介绍了使用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读取文件的第一部分

本文发布于:2023-10-31 06:51:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一部分   文件   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!