Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:08:01+00:00 2026-06-02T16:08:01+00:00

I get an error when trying to download a pdf with java. I know

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:08:01+00:00Added an answer on June 2, 2026 at 4:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to download a file from the internet, but I get the error
I get an error when trying to insert into TABLE_EVENTS but no problem inserting/querying
I get the following error when trying to run my application. There are no
I get this error when trying ruby script/console Rails requires RubyGems >= . Please
I get this error when trying to link a win32 exe project. I have
I get this error when trying to load a Zend Framework application: Fatal error:
i get this error when trying this: ERROR method name expected. How should i
I get following error when trying to access Xampp from a network I've tried
I always get this error when trying to compile my file with Boost::GIL PNG
why would I get this error when trying to do a local install of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.