gpt4 book ai didi

java - HTTP GET 检索压缩文件

转载 作者:可可西里 更新时间:2023-11-01 16:25:50 25 4
gpt4 key购买 nike

我开发了一个 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 方法。

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

干杯弥敦道

最佳答案

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

尝试切换到 BufferedInputStream反而。使用任何 Reader 类都会损坏二进制数据。

关于java - HTTP GET 检索压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12516532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com