Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve an image using iText when the URL has redirects?

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?

like image 584
Caroline Orr Avatar asked Dec 22 '22 13:12

Caroline Orr


1 Answers

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.

like image 101
Jamie Love Avatar answered Dec 28 '22 07:12

Jamie Love