Render image using OutputStream In FOP - java

I wanted to render image on PDF which is going to generated by FOP. To render image I am using tag <fo:external-graphic> As below :
<fo:external-graphic src="url('../offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8')" inline-progression-dimension.maximum="100%" content-height="scale-down-to-fit" content-width="scale-down-to-fit"> </fo:external-graphic>
I also tried by giving full URL as:
<fo:external-graphic src="http://10.2.10.79/Web/offlinePaper/displayImage?disImg=e391d672-ebf4-44d8-86cb-2cf987a50bf7" inline-progression-dimension.maximum="100%" content-height="scale-down-to-fit" content-width="scale-down-to-fit"> </fo:external-graphic>
In controller I am having an request mapping (../offlinePaper/displayImage) which takes image name disImg from getParameter and decrypt the image and return in OutputStream.
But when I generate PDF I found Error in log file as:
Image not available. URI: http://10.2.10.79:80/Web/offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8. Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for http://10.2.10.79:80/Web/offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8 (No context info available)
But if I copy this URL and paste in URL then image get shown in browser and I also found that when I generate PDF the request mapping which is suppose to get call for image decryption is not getting called.
Update
Also a strange think I come to know that with same code PDF get successfully generated if web server is Tomcat but if I deploy application on Glassfish it's giving error.

In the exception trace stands:
ImageException: The file format is not supported
The FO seems to be able to find an ImagePreloader according to file extension. If it doesn't find any, it breaks.
I'd propose to change the way how the url is built. There should be a real image file name included.
So, instead of:
../offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8
try
../offlinePaper/displayImage/disImg4bec89f0-5b97-40c3-b7c9-ac555a664df8.jpg

Related

Wicket 6.3 FileUpload uploaded file is distored when downloaded or viewed randomly

My application uses wicket 6.3 and my file upload is working as expected, except for some cases of corrupted or distorted files when viewed or downloaded.
In most cases, file upload is working, but there are cases when the uploaded files are saved (no error), but cannot be viewed or downloaded correctly since it is corrupted.
The file size of these files are just less than 100KB.
When I say corrupted, when you try to open the uploaded file it will look like this:
Below is the part of my code that saves the uploaded file:
BE code
FileUploadField fileUploadField = new FileUploadField("fileUploadField");
...
// File types can be images (jpg/png/bmp), documents (docx/pdf)
// File types with random distorted/corrupted (png and docx)
// Reuploading the file will fix the issue
for (FileUpload fu : m_fileUpload.getFileUploads()) {
byte[] fileByes = fu.getBytes();
String fileId = myService.persistFile(new MyFile().setContent(fileByes));
supportingDoc.addAttachment(fileId, fu.getClientFileName(), fileByes.length);
}
...
HTML
<input wicket:id="fileUploadField" type="file" class="form-control">
When I try to replicate it again, it usually works, and I can download or view the file.
I am unsure why it gets corrupted/distorted and how I can avoid getting a such an error though it rarely happens.
Update:
I thought the initial case was a PNG upload but it was not, it was docx. When I tried to replicate the issue using PNG file. It works.
I have 2 tomcat servers (test and live). I uploaded the same docx file on both tomcat servers (both on ubuntu). Test server was able to view/download the uploaded docx file, while Live server did not.
I am converting the file into byte array and save it to DB. When I compare both file contents in DB, they have exactly the same content. So the problem is not really on uploads.
I think the problem is on the download decoding, both servers does not have the same decoder. On my local environment (tomcat + windows) it works, same with my Test environment (tomcat + ubuntu). My Live environment running tomcat + ubuntu seems to have a different default decoder that is why it cannot view/download docx properly.
My problem now is where and how I can check that default decoder? Will I check it in ubuntu side? or it should be in tomcat side? When I checked the tomcat server config on both Test and Live tomcats, both seems to have the same config. They only differ on SSL certs.
Solution that works on my case:
The problem was really on how my Live server handles unknown mime-types. It is handling it like text file. This is the reason why it is showing the unknown mime-type file as garbled text.
I checked my Live tomcat server configuration and compared it with my test server's config and both are almost the same and I cannot find any configuration relating to mime-type or encoder/decoder.
In my file download code, when mime-type is unknown it is setting it as force-download only so I changed it to application/force-download.
For unknown mime-type, I changed the content-disposition from inline; filename="<filename>" to attachment; filename="<filename>".
I think content disposition attachment alone works, I haven't tested it though since application/force-download seems to be a hack, while setting the correct content disposition as attachment will download the unknown mime-type for my case.
I will not delete this question though, since I might have the same issue in the future and I forgot how I solved it :)

HTTP header for inline PDF filename from Java webserver

I need to send to the client a byte[] with a pdf data from my tomcat server.
I'm using this:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition:","inline; filename=test.pdf");
But (at least) with firefox I get a file download instead of inline display.
The only way to show pdf data inline is to remove the Content-Disposition header record however, if I do so I cannot set the filename, the pdf name is get from the last folder of url.
You seems to be setting the right headers. But rendering of pdf or another such formats depends on the browser capabilities as well. I mean browser need to have a pdf plugin installed in order to render a pdf when it sees the same in the contentType header field. So make sure you install a pdf plugin for your firefox and try to test after that. You can download firefox pdf plugin from here:
https://addons.mozilla.org/en-US/firefox/addon/pdf-download/

Issue showing image on UI when retrieve from outside of project

I have a Java project upload functionality which is uploading image onto the server location C:/temp and I want same image to show on UI as soon as uploaded but the problem is when i am passing image path(C:/temp) into JSON then then system is reading that image path relative to project and getting an error
(NetworkError: 404 Not Found - http:// localhost:8080/group/images/c:/temp/Jellyfish.jpg")
because file is present in C:/temp. What would be the possible solution of this. please help me guys?
Thanks,
Ankit
You need a servlet that will read the file from c:\temp, and send the read bytes to the servlet's response output stream. You will also need to set the content type of the response to the mime type of image you're sending (image/png for example), and the content length to the number of bytes in the file.
And this is the URL of this servlet that you will have to pass into the JSON object.

smartgwt save image file instead show in browser

I've got a smartgwt application which create a link with a jpg/gif/png/pdf files. This files are shown in browser. I want to get the save dialog instead it which ask me the path when I want to save the file at local machine. How could I do that?
As I know, you have to change the response header by setting the Content-disposition to attachment. Like this:
'Content-disposition: attachment; filename=image.jpg'
'Content-type: image/jpeg'
With these the browser will understand most cases that it should show up a dialog to save the image with the name: image.jpg. Also it might offer you to send it directly to an application, for example to an image viewer.
To get it work from a simple link, perhaps you have to write a servlet which will return the requested file with the correct headers and call that servlet from every link with a parameter to the real file.

Display Image on web page using Java

In my Java EE app, there is a function to upload images. When uploading an image, I'm saving image path in MySQL database.
Now I want to display uploaded image on a web page using image path that saved when uploading the image in my MySQL database table.
How could I do this ?
It depends. Is the path someplace that a web server is serving documents from? Then just include the path, adjusted as necessary, in an IMG tag. If not, or if the image data itself is actually in the database (you're not entirely clear about this) then create a servlet which returns the contents of an image based on query parameters, and use that servlet's URL (plus the query parameters) in the SRC attribute of the IMG tag.
Write a servlet that extracts your stored image from database and writes back into servlet outputstream.
You need to set the relevant mime type of the image, for example "image/jpeg", before writing into outputstream.
You need to point the image source to this servlet url with required input parameters to load the correct image from the database. For example :
<img src="http://mydomain/servlet/imageServlet?imgid=xyz" />
There is nice example given by BalusC at: ImageServlet serving from database

Categories