-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi I don't have a lot of experience that's why I'm here to ask some help. I get a String from the server which contains HTML to display. It works well but I need to display pictures sometimes (like img src="./images/mypic.jpg"/>).
So I decided to load every picture I need to display from the server and to save them into a folder that can be opened when HTML is read.
I don't know how to do it, I tried to save pictures with this path : Executions.getCurrent().getDesktop().getWebApp().getRealPath("/")
But question is still how can I find my pictures ?
Thank you
EDIT : SOLUTION
I found a solution : When I create my file, I use this method :
public void saveAsFile() {
try {
BufferedImage pic = ImageUtils.decodeToImage(this.getImage());
String path = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/ressources/");
File outputfile = new File(path.replaceAll("\\\\", "/") + "/pics/tmp/" + this.getNom_image());
ImageIO.write(pic, "jpg", outputfile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Then my picture can be created in webapp resources folder. And when I read the content of my HTML, I replace src with this expression :
contentHTML = contentHTML.replaceAll("src=\"./images/","src=\"./ressources/pics/tmp/");
It works on local and when I access to my computer with my tablet on web browser.
Don't know if it is the best way of doing it, but it works for now.
Thank you
Hello fredp,
If i may suggest a different approach, you could read the images from the database and display them directly as shown in the following tutorial, and post:
https://www.ibm.com/developerworks/community/blogs/gbowerman/entry/uploadinganddownloadingimagesto?lang=en
http://forum.zkoss.org/question/72119/displaying-image-from-database/
There is no need to do the extra step of copying the images to a directory.
Best Regards,
Darksu
Hi Darksu and thanks for replying. I think that can be a great solution but not to my problem. Maybe I did not understand well but : I can't set a Media like in the example. I don't know when I have to display a picture, it is in my HTML String content (I use regular expression to find img src="...").
So it has to stay in HTML. That's why I need to set an address. I tried with Executions.getCurrent().getDesktop().getWebApp().getRealPath("/ressources/"), even with local address it works great when I am running my app into Eclipse. But when I run it into web browser the picture is not displayed. After inspecting the element I found that message in chrome console : Not allowed to load local resource:
If found a topic where it says :
Probably you open index.html with file:// protocol, Chrome default security policy doesn't allow you to open local files except one that you have in window.location.
I suggest to use http://
Then I am trying to find how to have an http address of local files
Thank you
Hello fredp,
Usually i create a directory where i store the log files for my application. So Why not create a directory that will be used from your application to load the images? It's simpler if you think about it.
For example you could create the following:
/opt/ApplicationName/images/
Best Regards,
Darksu
Asked: 2015-05-29 09:19:38 +0800
Seen: 20 times
Last updated: Jun 04 '15