La Chamelle La Chamelle - 1 month ago 17
Java Question

Accessing external URL resources in iText

I'm working on a JSF 2.0 project using Mojarra, PrimeFaces and Tomcat 6.x, but in front I have an Apache HTTP server.

I created a web form where I can select PDF files which I want to merge. These files are external of my WAR in a directory under Apache HTTPD's control. I use iText 2.1.7 to merge the PDF files.

At the moment I'm accessing the files as follows:

PdfReader reader1 = new PdfReader(new URL("file:///appli/Vignette/vcm/inst-vgninst/docroot_CDC" + file));


However, I want to access them by HTTP:

PdfReader reader1 = new PdfReader(new URL("http://centos" + file));



  • centos is the name of my server, where the webapp is deployed.

  • file is a string variable look like "/folder/folder1/file.pdf"



This fails. But the url http://centos/folder/folder1/file.pdf is accessible by a normal webbrowser.

What's wrong and how can I fix it?

Answer

The PdfReader apparently can't work directly with URLs which point to an external resource. If you really intend to work with URL rather than File or FileInputStream, then your best bet is to use URL#openStream() to return an InputStream to the PdfReader. It has namely also a constructor for that.

PdfReader reader1 = new PdfReader(new URL("http://centos" + file).openStream());
Comments