I'm using the h:graphicImage tag in JSF. Is there a way to show a blank image (i.e. blank.jpg) if there is a broken or not found image?
There are two ways:
client side - with javascript, see here
server side (less preferable) make an ImageServlet which takes an image url, loads it and sees if it exists. If it exists, it is streamed to to client. If not - the blank.jpg is streamed to the client. Verification can be done via new File(fullPathToImage).exists(), or, if the image is not on your server, using URL.openConnection().
Related
Hi i have a requirement to send a daily email to customers with latest information. This weather information is on an image on the message body. The readings on the image will be dynamic. Any pointers on how i can achieve this.
Same image is below
As seen above all the text will change dynamically based on the weather forecast and this has to go as a mail body.
I use java for programming, but i am ok to use any programming language based on suggestion on how this can be archived quickly
You can create inline images within HTML like this: https://en.wikipedia.org/wiki/Data_URI_scheme .
A little more effort is required to create MIME attachments, which you would reference from your HTML. This also results in inline images. How to create an email with embedded images that is compatible with the most mail clients
These are two alternatives for creating a dynamic image at the time of your email's creation. For more dynamic behaviour, you would either use proper image urls (which will be blocked by clients) or you could use Javascript (which should not work for any client for security reasons).
I need to display html content in WebView control. Html content is served from rest webservice as string and it may contain images, css etc that must be loaded from server.
Obviously, I could let WebView to load all content automatically, but it's not good solution in my case. I need html content to be displayed immediately. So, before I load it into WebView I need to cache it first, probably just after webservice call and provide already cached data for display.
So I have question - what is simplest way to meet my requirements? Is there any built in mechsnism that I can use in my case?
please try this first and if it doesn't fit your needs you can go for some server side solutions like this one. as another way you can store your html content in database and use it as a cache, but you should think for a way to invalidate expired caches.
I've been struggling with this for the last few hours and can't seem to figure it out at all. To start I'll lay out what I'm trying to do.
I want to make it so that when you generate a craft idea on this page
http://craftspo.com/craft-idea-generator/
it saves an image of the idea on my server (so that I could then allow the user to pin it on Pinterest, share it on FB, and so on).
So far, I've managed to get it so that when they click the button, it uses html2canvas to snag a snapshot of the table. The problem is that image ends up being in base64, so it's pretty much unusable.
From what I've read, you can really only decode (and then save) base64 images to a server using PHP. And of course PHP is only called whenever you load the page but I'd rather not make them reload the page every time they generate a new idea. So is there anything else I can do? Even if it means doing something completely different. At this point, I just wanna make it happen so I can feel like I conquered the problem!
if I recall correctly base64 or image has no different,
just like the comment above
you could use it like :
<img src="data:image;base64,etc"/>
if you don't want the user to reload, you could use ajax to replace the src using javascript/jquery, you could just replace it with base64 image instead link to image.
if the image ain't that big, I myself would prefer to use base64,
you could also save the base64 string into database, if it could hold the length :D
and use it later on
I have a PHP page that grabs a variable via GET and then pulls in some information from a database based on that variable. Once finished with the server-side stuff, there is some javascript that runs and takes the data supplied and creates a .png image using a 3rd party API and then saves that image to my server using an AJAX POST call to another PHP page.
That all works fine, but what I'd like to do now is automate some calls to that PHP page. Namely, say I have 100 such variables to go through, I want to, preferably in Java with a for loop, call that PHP page with each variable in turn.
The problem is that client-side javascript. It won't execute with the simple URLConnection in Java. It seems like I need some sort of browser replicator or some way to have java act like it's calling the PHP in a browser?
Alternatively, I could make do with having a third PHP page act in place of the Java as the controller, but I'm faced with the same problem of getting the javascript to execute.
Am I missing something easy? Is this set up not possible? I'd really prefer to do it in Java if possible to fold it into other code I already have running.
Let me try to add some more specifics without bogging it down too much. There's a PHP file getData.php that takes in an ID number via GET. So I call it like ./getData.php?id=someId
That PHP file takes the ID, goes to my DB and retrieves some data and pastes it into the HTML source. Then once the page is finished, I have some javascript within getData.php that retrieves that data, formats it into a DataTable and passes it off to Google Visualization API in order to make a SVG chart.
Then I have more JS that runs that takes that SVG object, turns it into a Canvas object, grabs the base64 image data from it and finally POSTs to saveTo.php with the following array:
{'id' : id, 'data' : imgData}
saveTo.php simply takes in that POST data, creates a file on my server based on id and pastes the imgData into it. The end result is that I can pass in an ID to getData.php and end up with saved image of a Visualization chart that I want made based on data in my DB tied to that ID.
That all works by hand. But I have ~1,000 of these IDs and I'd like to have it so this whole process is run each morning so that I can have updated images based on yesterday's data.
I should mention that I did try using the 3rd party toolkit HtmlUnit (http://htmlunit.sourceforge.net/) but just keep getting these errors:
com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Some more searching around and hitting upon the right keywords to try finally led me to Selenium, which is exactly what I was looking for.
http://docs.seleniumhq.org/projects/webdriver/
I have ByteArrayOutputStream which contains a JPEG image in bytes. My requirement is to display that image in a JSP page (to display the image in the frontend using HTML tags). How do I do that?
I have referred the BufferedImage class, but it is confusing for me because I am new to this.
If the image is not too big, you can do it as follows.
<img src="data:image/jpg;base64,iVBORw0KGgoAAAANS..." />
Where iVBORw0KGgoAAAANS... is the Base64 encoded bytes.
Base64 encoding can be done with a library, like Ostermiller Java Utilities' Base64 Java Library or org.apache.commons.codec.binary.Base64.
Unless you use a "data" URI (useful for small images), the browser will make two requests: one for the HTML and one for the image. You need to be able to output an img tag which includes enough information to let you respond to the subsequent request for the image with the data in your ByteArrayOutputStream.
Depending on how you got that JPEG file and how your server scales out, that might involve writing the image to disk, caching it in memory, regenerating it, or any combination of these.
If you can delay the image generation until the browser requests the actual image in the first place, that's pretty ideal. That may involve putting extra parameters in the URL for the image - such as points on a graph, or the size of thumbnail to generate, or whatever your image is.
If you're new to both JSP and HTML, I strongly recommend you concentrate on the HTML side first. Work out what you need to serve and what the browser will do before you work out how to serve it dynamically. Start with static pages and files for the HTML and images, and then work out how to generate them instead.