File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Writing image into WAR? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Writing image into WAR?" Watch "Writing image into WAR?" New topic
Author

Writing image into WAR?

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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 ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can use ClassLoader.getResourceAsStream to get a stream from a resource contained in the WAR file.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
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.


Ping & DNS - my free Android networking tools app
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing image into WAR?
 
Similar Threads
Auto display uploaded image
Loading problem with t:inputFileUpload
Showing image from servlet
In webApps how to create a new image and present it through a html ?
Writing Image Bytes on Response Stream