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
Related
I have .vm file for confirmation order mail and I'm placing some informations to this .vm template. There are several images in these informations. But my images not rendered while the email was sending.
I tried several methods but doesn't work.These are:
<img src='${ctx.contextPath}/images/theme/social_01.jpg
<img src='${ctx.themeResourceUrl}/images/theme/social_01.jpg
and
<img src='../images/theme/social_01.jpg
Any one of these are didn't work for me.
Also my images at the following location of the project:
xxxStore > import > sampledata > contentCatalogxxx > images > theme >
How can I solve this problem ?
Can you print the complete image URL in the email and try to open it in the new browser window and see. You should able to access your image. If not, try looking at the URL, is it correct? Let me know the result.
I would suggest you to following any exiting working image, place your image to the same folder, and access it similarly.
It seems you are trying to access static image. You can find all frontend static content under the webroot folder of the storefront extension. Your image should be in the same folder as well.
e.g.
I have an image at
sastorefront/web/webroot/_ui/responsive/theme-blue/images/test.png
I can access it using
${themeResourcePath}/images/test.png
To debug path issues, I would print the path, and try hitting same URL through the browse. Ideally, an image should be accessible with the URL. Say in my case, I can access test.png with
https://example.com/_ui/responsive/theme-blue/images/test.png
You have to make sure that your image URL which you are using in VM is corresponding to the actual image URL.
To provide this,
go to Backoffice media type
check your media URL
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
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.
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.
I have a servlet which returns as response an html page that also includes pictures. I have those pictures stored on the file system in the Pictures folder (path: /home/andrei/Pictures).
I currently have there just one photo(name: eu.jpg ) but i will add more.
So the point is that in the html code i somewhere have this: <img src="/home/andrei/Pictures/eu.jpg" alt="pic" > . But when i get the html page it doesnt display neither the photo nor the "pic" text from alt(not sure if this is normal...). I read that it may be due to the path i gave in src but i dont know exactly what to give.
So what path should i give to the src?
And is it normal not displaying the alt text because i knew that when it cannot load the pic it shows that text.
Additional information:
IDE : Eclipse Juno
SO : Linux
Server: Tomcat 7.0
Any image you intend to display in a browser connecting a web container (Tomcat in your case) must be visible in the container. To do it, place the images in the webapp/ folder and link them properly from the servlet generating the correct tag where path is the correct http link to your file.
When you send back the HTML response, the client's browser will render the document and then, send requests for additional resources in order to show the HTML document fully. That includes the images, so Tomcat must be able to serve them. If you can't put them in a folder inside WebContent from the beginning, the servlet must do the move and create the document specifying the "public route".
The images should be part of the webapp. Absolute FS path will not work. <img> will take relative path, or absolute path wrt the website.