views:

1759

answers:

3

I am using iText to generate PDF invoices for a J2EE web application and included on the page is an image read from a URL constructed from the request URL. In the development and test environments this works fine, but in production I get a java.io.IOException: is not a recognized imageformat.

If I paste the url into my browser then the correct image is returned, however the request is redirected from http to https. In my code if I hard code the redirect URL then the image is displayed correctly.

So it seems that when retrieving the image using com.lowagie.text.Image.getInstance(URL), the redirects on the URL are not being followed. How can I output an image from a redirected URL using iText?

+1  A: 

Is there a reason you have to get this using the URL?? Do you have to match the image that the url is pointing to? What if it changes or gets removed?

I am not sure of your requirement, but it may be easier to save the image from the url and place it somewhere within your project. Then you can add it to your pdf with:

Image.getInstance("yourimage.gif");
CFreiner
+3  A: 

Well,

If you ask for an image from a URL, it must actually point to the image. If the URL points to a web page that then redirects to another URL (or the return code from the URL is a redirection), then it is going to fail.

This is essentially due to the getInstance() method understanding how to use the HTTP location protocol to get a file, but not understanding the HTTP protocol enough to be a HTTP client.

You could just use the 'https' address, or you could store the image with your program and locate the as CFreiner suggests. If neither of these options are feasible, then your only real solution is to implement code to query the URL, check if it is a redirection and if it is follow the redirection.

Jamie Love
A: 
When you have multiple redirects u cant use url. instead of url you can specify the path of the image in Image.getInstance("path"). u can plcae the gif file in your deployment package.
Ananthan