如何从java中读取HttpEntity和BufferedHttpEntity中的所有内容

时间:2014-02-23 19:43:14

标签: java http buffer

我写了一个用于在java中发送txt文件的HttpClient。一切都很好,但在服务器端,我只收到一小部分文件。我已经google了,事实证明java HttpClient有一个8KB的缓冲区,因此解决这个问题的方法是使用BufferedHttpEntity。但我仍然无法弄清楚如何使用它。有人能帮助我吗?这是我的服务器端代码:

public void handle(HttpRequest request, HttpResponse response,
        HttpContext context) throws HttpException, IOException {
    String responseBack = "";

    HttpEntity entity = null;

    // Check if the request has an entity
    if (request instanceof HttpEntityEnclosingRequest) {
        // If it is, get the entity from it
        entity = ((HttpEntityEnclosingRequest) request).getEntity();
    }

    BufferedHttpEntity buffEntity = new BufferedHttpEntity(entity);

    InputStream is = buffEntity.getContent();
    String s = getStringFromInputStream(is);

    // Log the data received
    Log.i("Data Received", s);

    // Make a simple response back using the data received
    responseBack = "Successfully received!";

    // Create a new entity for the response and then set it
    HttpEntity entityResponse = new StringEntity(responseBack, HTTP.UTF_8);
    response.setEntity(entityResponse);
}

0 个答案:

没有答案