2014-10-06 63 views
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) 
+0

请问你格式化你的代码,当你问问题。 – 2014-10-06 11:55:36

+0

错误发生在哪一行? – 2014-10-06 12:00:47

回答

1

您使用的是临时的字节数组可能是问题的原因,以下载大量数据时。 您可以直接将流的内容写入您的文件。

String uri = ""; 
getMethod = executeGet(uri); 
httpClient.executeMethod(getMethod); 
InputStream istream = getMethod.getResponseBodyAsStream(); 
IOUtils.copy(istream, new FileOutputStream(new File("xxx.zip")); 
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(); 
} 
相关问题