• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing image into WAR?

 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am finding it difficult to create and write an image into WAR file and get it through a jsp or html page.

How can I write a image into WAR deployment folder?

My requirement is upon request for a page, I have to get data and create a chart which I have to save it as png to display it along with some table.

Please help me in this regard.
[ May 04, 2008: Message edited by: Bear Bibeault ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use ClassLoader.getResourceAsStream to get a stream from a resource contained in the WAR file.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you really mean that you want to write the folder to the war file. A war file is an entire web application zipped into a single file.

I assume that you want to write the image into the application's directory structure. The answer is... it depends.
Depending on your container, how it's configured, and how you've deployed your application, there may or may not be a directory structure to write to.

If your container unpacks your application before deploying it, you can find the root of your application's directory structure with servletContext.getRealPath("/"). If not, this method will return null because there is no file structure.

I recommend either streaming the generated image directly to the browser if it's only going to be used once or, if you need to access it more than once, writing it to a directory outside of your web container and/or your web application's directory structure. Then, write a servlet that can read this file and stream it to the browser. Either of these two solutions will be more robust and portable because they don't rely on the application being deployed in any particular way in order to work.

By the way:
We have an example application with a servlet that streams images in our Code Barn.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is upon request for a page, I have to get data and create a chart which I have to save it as png to display it along with some table.


Writing to your WAR is not the solution for this. It appears that you have to create this data dynamically. Perhaps you are looking for something like this

http://code.google.com/apis/chart/

You can also use the jFreeChart API and couple it with an API like iText if you want to create PDF content or charts using a java API. I wrote a little article about doing this. Check it out if you are interested

http://jtoee.blogspot.com/2008/04/writing-pdf-charts-with-itext-in-less.html
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther ,

you got my point but still, its not perfect solution for me.

Here is why :

1. I have to embed the image into a html page.
(Displaying the image is not the only purpose.)
I mean the content type response page is "text/html". (Not image/png)

John,
I have alreay used Jfree chart to genrate the chart.
I am not struck in generating the chart but "to embed image in a html response."

David O'Meara ,

can i create a empty image say by name "myImage.png" into images folder of webApp. SOthat I can open the image and write it into it?


Please reply me...
[ May 06, 2008: Message edited by: Srinivasan thoyyeti ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not perfect solution for me. Here is why :

1. I have to embed the image into a html page.
(Displaying the image is not the only purpose.)
I mean the content type response page is "text/html". (Not image/png)

It actually sounds like a perfect solution. What Ben suggested was to write the image to a file, and then have a servlet stream it to the browser. So in your HTML you would have a tag like this:

<img src="/myWebApp/ImageStreamingServlet?img=xyz.png">

Then all the ImageStreamingServlet needs to do is to load the image and stream it to the browser with its content type set appropriately. See the CodeBarn example Ben linked to for how this works.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer,

Thanks a lot.
I am not good at HTML.

I am thinking of img tag's src attribute as path to static png file.
I didn't thought of calling a servlet inside a src attribute.

This is due to lack of knowledge in HTML.

Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic