将HttpEntity写入文件

时间:2010-02-26 15:27:28

标签: java apache-commons-httpclient

我正在尝试使用jakarta commons HttpClient库。

我觉得我在这里很蠢,但我无法弄清楚如何写一个完整的HttpEntity来存档。

我正在尝试:

FileOutputStream os = new FileOutputStream(f);

e.writeTo(os);
while (e.isStreaming()) {
   e.writeTo(os);
}

其中e是我的HttpEntity,f是我的档案。我只得到任何文件的前8KB,我想是因为在某处缓冲。知道我如何得到其余的吗?

2 个答案:

答案 0 :(得分:1)

解决了它。

我需要强制响应对象使用BufferedHttpEntity:

HttpEntity entity = rsp.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(entity);

答案 1 :(得分:1)

response = httpclient.execute(get);
is = response.getEntity().getContent();
IOUtils.copy(is,fileWriter);
相关问题