Issue showing image on UI when retrieve from outside of project - java

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.

Related

Let user choose file type while saving a file in Web application

I'm working on a web application at the moment using Tomcat and spring framework. A CSV file is stored on the server, the path to the file and the filename is stored in the database.
A search button on a webpage would list all records in database, if user click on one listed item, a saving dialog would be displayed and the file will be saved locally.
I want a dialog like this opened when user click on one item, how can I do it with httpServlet? I can think about a solution is set content-disposition type in response header to 'attachment'. It would force the browser to download the file instead of trying to display it in browser. But I want user to be able to select the type for file download. Please take a look at the image below
Setting the Content-Type response header to something that the browser is unable to render should prompt the user to save the file upon receiving the response. A value of application/octet-stream (arbitrary binary data) should do the trick.
However, since you expect the actual file content to be in different format depending on user choice, here's what you would need to do:
create a link/form on your html page that will allow user to select a type and make a request to URL with proper extension (like download/file.xls for XLS or download/file.csv for CSV).
in your servlet that handles these URLs, check the extension requested (easy) and then convert the file to expected format within your servlet (not so easy) and send it in response.

Render image using OutputStream In FOP

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

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

POST a XML file content to a HTTPS url

I want to send a xml file through POST request to a HTTPS server.
I think i have to read the content of the XML file and save it to a String, then send POST it to the server.
Sorry, but i don't have any code to demonstrate my workings. Can someone help me to do a Sample code or tutorial to start with this task ?
Note : I am using Java
Using java, You can open this XML file in READ mode and start reading and printing contents in browser.

Categories