0

I use some Apache Commons libraries. In particular, I'm using version 4.2.5 of httpclient and version 2.4 of commons-io. Thus, using a DefaultHttpClient I try to download pdf files from URLs. E.g. Suppose that I want to download this file: http://www.lowaste.it/files/pdf/C12-02_filiere.pdf. I get a 200 response, but as one may see by using a browser, the file is never loaded. It seems that the bytes are never loaded after the first answer.

In my code, I do as follows:

HttpEntity entity =//... I get the entity via DefaultHttpClient
InputStream in = entity.getContent();
FileOutputStream out = // init the OutputStream
org.apache.commons.io.IOUtils.copy(in, out);

The function IOUtils.copy seems to loops infinitely and never stops. I stayed ours waiting for the file to be saved. The IOUtils.copy function uses IOUtils.copyLarge. I report the function for the sake of completeness:

public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
        throws IOException {
    long count = 0;
    int n = 0;
    while (EOF != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

I also tried to replace IOUtils.copy(in, out) with:

IOUtils.copy(IOUtils.toBufferedInputStream(in), out);

since object in relies on a network connection, but it doesn't helped!

Any idea to resolve the problem?

mat_boy
  • 10,930
  • 19
  • 62
  • 103
  • The only solution that I found is to use a Timer that closes the streams after a period (e.g. 5 minutes). I only have to handle the `NullPointerException` eventually thrown by `copyLarge`. However, this solution seems to be a little tricky! – mat_boy Sep 11 '13 at 09:26
  • If I understand you correctly, you should use `HttpConnectionParams.setSoTimeout()` to set read/write timeout for the socket. For watchdog the solution is given [here](http://stackoverflow.com/a/9873902/267197). – dma_k Nov 12 '13 at 14:56

0 Answers0