【问题标题】:HTTP GET to retrieve compressed filesHTTP GET 检索压缩文件
【发布时间】:2012-09-20 16:04:50
【问题描述】:

我开发了一个 HTTP 通信对象,用于通过 GET 请求下载文件。

这在下载文本文件时工作得很好。但是,下载 zip、gz 或 tar.gz 等压缩文件时似乎下载了该文件,但该文件无效。

在 zip 的情况下,我收到一条消息,说它试图在文件开头之前移动指针。 在 .tar.gz 的情况下,消息是 file.tar 中的数据错误。文件已损坏。

在所有情况下,我使用的下载链接都允许从 URL 进行完整且正确的下载。然而,基于 Java 代码的下载将文件下载下来,但它是无效的。

代码如下:

public class HTTPCommunicationGet {

    private URIBuilder sendData;
    private URI target;
    private HttpGet getConnection;

    public HTTPCommunicationGet(String url, TreeMap<String, String> components) {
        super(url, components);
    }

    public HTTPCommunicationGet(String url, String queryString) {
        super(url, queryString);
    }

    protected void defineSendData() throws URISyntaxException, IOException {
        this.sendData = new URIBuilder(new URI(this.getUrl()));
        if (this.getComponents() != null && this.getComponents().size() > 0) {
            for (Map.Entry<String, String> component : this.getComponents().entrySet()) {
                this.sendData.setParameter(component.getKey(), component.getValue());
            }
        }
    }

    protected void retrieveRemoteData() throws IOException, MalformedURLException, URISyntaxException, DataMapHTTPGetException {

        this.target = this.sendData.build();
        this.getConnection = new HttpGet(target);
        HttpResponse response = client.execute(this.getConnection);
        if (response.getStatusLine().toString().toUpperCase().contains("200 OK")) {
            this.setResponse(response.getStatusLine().toString(), "Data Retrieved");
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
                this.remoteData.append(line);
            }
        } else {
            String message = String.format("%s: Provider connection exception; response returned was not 200 OK", this.target.toASCIIString());
            this.setResponse(response.getStatusLine().toString(), message);
            DataMapHTTPGetException ex = new DataMapHTTPGetException(target.toString(), message);
            throw ex;
        }
    }

    public void downloadFiles(String localFile) throws DataMapConnectionException, FileNotFoundException, IOException, URISyntaxException {
        // check that we have remoteData set
        this.defineSendData();
        this.retrieveRemoteData(); // everything is bubbled up to the controller class that is calling this.

        File localMetaFile = new File(localFile);
        switch (this.archiveMetaFile(localMetaFile)) {
            case -1:
                IOException ex = new IOException(String.format("The file %s could not be moved", localFile));
                throw ex;
            //break;
            case 0:
                infoLog.info(String.format("%s: this file did not already exist", localFile));
                break;
            case 1:
                infoLog.info(String.format("%s: this file was found and successfully archived to the processed directory", localFile));
                break;
        }

        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(localFile));
        fileWriter.write(this.remoteData.toString());
        fileWriter.close();
    }
}

如您所见,这是在对象初始化后通过 downloadFiles 调用的。我已经删除了本示例不需要的代码,例如 archiveMetaFile 方法。

非常感谢任何关于为什么这不适用于压缩文件的指针。

干杯 内森

【问题讨论】:

  • "response.getStatusLine().toString().toUpperCase().contains("200 OK")" -- 不要那样做;原因短语不需要是“OK”。响应有一个整数状态码获取器是有原因的。
  • 支持该评论 - 不是实际问题,但非常有帮助 - 谢谢。你以后可能已经拯救了世界的痛苦。

标签: java http zip gzip


【解决方案1】:

问题很可能是您使用的是BufferedReader 而不是InputStream。 Readers 用于文本数据并强制进行字符编码,而 InputStreams 可以处理原始二进制数据。

尝试改用BufferedInputStream。使用任何 Reader 类都会损坏二进制数据。

【讨论】:

  • 太棒了!非常感谢你做的这些。我现在已经实施了这些更改,一切都很好。这当然是一些需要锁定和反复使用的知识。由于 BufferedReader 和二进制数据的额外信息,答案已被接受并给予支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多