代码之家  ›  专栏  ›  技术社区  ›  Adrian Mouat

正在将httpentity写入文件

  •  2
  • Adrian Mouat  · 技术社区  · 15 年前

    我正在尝试使用Jakarta Commons HTTPClient库。

    我觉得我在这里很笨,但我不知道如何将完整的httpentity写入文件。

    我正在尝试:

    FileOutputStream os = new FileOutputStream(f);
    
    e.writeTo(os);
    while (e.isStreaming()) {
       e.writeTo(os);
    }
    

    其中e是我的httpentity,f是我的文件。我只得到任何文件的前8kb,我想是因为在某个地方进行了缓冲。知道我怎么得到剩下的吗?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Adrian Mouat    15 年前

    解决了。

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

    HttpEntity entity = rsp.getEntity();
    BufferedHttpEntity buf = new BufferedHttpEntity(entity);
    
        2
  •  1
  •   CoolBeans Jake    13 年前
    response = httpclient.execute(get);
    is = response.getEntity().getContent();
    IOUtils.copy(is,fileWriter);