hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nadeem ghani <ghani_nad...@yahoo.com>
Subject Re: entity.getContentLength() and entity.getContent() don't match
Date Sun, 12 Aug 2007 00:34:46 GMT
Thanks Roland.

But your suggestion doesn't seem to be working. Invoking Entity.getContentLength() returns,
correctly, 80MB of data to be read, but when I read from the InputStream, I only get a few
kB.

Also, if I use curl I do get 80MB, so the data should be there.

The code I'm using is below. If you could point out errors, or suggest anything else to try,
I'd appreciate it.

Nadeem

    public static void writeToFile(HttpResponse resp, String pathToFile) {
        HttpEntity entity = resp.getEntity();
        InputStream instream;
        try {
            instream = entity.getContent();
            if (instream == null) {
                System.out.println("instream == null");
            }
            System.out.println("entity content length: " + entity.getContentLength());

            long bytesRead = 0;
            FileOutputStream output = new FileOutputStream(new File(pathToFile));
            
            try {
                byte[] tmp = new byte[1024];
                int l;
                while((l = instream.read(tmp)) != -1) {
                    bytesRead += l;
                    output.write(tmp, 0, l);
                }
                System.out.println("bytes read: " + bytesRead);
            } finally {
                instream.close();
            }        
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }





       
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers
- Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message