0

尝试使用下载大数据时出现上述错误HttpGet

String uri = "";
getMethod = executeGet(uri);
httpClient.executeMethod(getMethod);
InputStream istream  = getMethod.getResponseBodyAsStream();
byte[] data = IOUtils.toByteArray(istream);
FileUtils.writeByteArraytoFile(new  File("xxx.zip"),data)
4

2 回答 2

1

您正在使用可能是问题原因的临时字节数组。您可以直接将流的内容写入文件。

String uri = "";
getMethod = executeGet(uri);
httpClient.executeMethod(getMethod);
InputStream istream  = getMethod.getResponseBodyAsStream();
IOUtils.copy(istream, new FileOutputStream(new  File("xxx.zip"));
于 2014-10-06T12:01:33.950 回答
1

您正在将整个响应读入byte[](内存)。相反,您可以在读取输出时流式传输输出,istream例如,

File f = new  File("xxx.zip");
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(f));) {
    int c = -1;
    while ((c = istream.read()) != -1) {
        os.write(c);
    }
} catch (Exception e) {
    e.printStackTrace();
}
于 2014-10-06T12:01:58.870 回答