I get an error when trying to download a pdf with java. I know there are similar questions, but not as specific as mine.
My code snippet:
URL url = new URL("https://.../abc.pdf");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
...
InputStream in= conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] buf = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
Ther servers response header:
X-AspNet-Version:2.0.50727
Transfer-Encoding:chunked
Date:Thu 26 Apr 2012 12:07:59 GMT
Content-Disposition:attachment; filename=abc.pdf
Set-Cookie:Language=en-gb; path=/
Connection:Keep-Alive
Content-Type:application/octet-stream
Server:Microsoft-IIS/6.0
X-Powered-By:ASP.NET
Cache-Control:private
The exception (at in.read(buf)):
Exception in thread "main" java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:556)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:600)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:687)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2959)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2953)
The code works for almost all cases and had been used thousands of times. But in very rare cases I get this exception. But I can download the pdf with the browser. Also if I put the pdf on my own server I can download it with my code. So it must have something todo with the way the server serves this pdf.
Maybe it has something to do with the Transfer-Encoding:chunked?
Has anybody an idea, what I can try to get this fixed?
It seems a bug related to java chunked handling. Many solved by reading one byte at a time and put the read in a try-catch for EOFException.