Apache HttpClient reading a URL and writing to a file is not reading the entire file in JBoss EAP

Solution Unverified - Updated -

Issue

  • java io stream not working correctly on Jboss EAP 6 or Jboss 7.1.2 on Linux. I am trying to read contents of a pdf from a URL by opening a connection using http client and then read the inpustream and write to ouputstream. The code works when run on some machines running on JBoss EAP and standalone clients, but does not work on one of our other machines. In successful case when I read the pdf from the given url and print the size, I see that we get around 36289 bytes.

Here is the code below:

  public void save(String docUrl, String outputFileName) throws FileNotFoundException, IOException {
    File outputFile = new File(outputFileName);
    DefaultHttpClient httpclient = createHTTPClient();
    HttpResponse resp = httpclient.execute(getHttpGet(docUrl));
    InputStream is = resp.getEntity().getContent();
    FileOutputStream fos = new FileOutputStream(outputFile);
    streamWrite(is, fos);    
  }

  protected void streamWrite(InputStream in, OutputStream out) throws IOException, FileNotFoundException {
    byte buff[] = IOUtils.toByteArray(in);
    out.write(buff, 0, buff.length);
    out.flush();
    try { out.close(); } catch(Exception e) {}
    try { in.close(); } catch(Exception e) {}
  }

  private HttpGet getHttpGet(String documentURL) throws Exception {
    HttpGet httpGet = new HttpGet(documentURL);  
    httpGet.addHeader("User-Agent", USER_AGENT);
    httpGet.addHeader("Accept-Charset", ACCEPT_CHAR);
    httpGet.addHeader("Accept-Encoding", ACCEPT_ENCODING);
    httpGet.addHeader("Accept", ACCEPT);
    httpGet.addHeader("Expect", EXPECT);
    httpGet.addHeader("Content-Type", CONTENT_TYPE);
    return httpGet;
  }

Environment

Red Hat JBoss Enterprise Application Platform (EAP)
Apache Http Client

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content