Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save jpeg image using sevlet and diplay it in html page

 
Meg Adal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one servlet which creates jpeg image.
I want to save the image in same folder where my servlet is and then diplay that image in the html page.
I wrote following code to create image
// here img is instance of class Myimg which extends canvas
// following code is in Myservlet class
BufferedImage myImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
img.paint(g2);
try
{
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e)
{
System.out.println(e);
}
If i wrote same code in class that extends frame(with some modification) the code works fine and saves the image in file with name specified
but the code is not working in servlet
Can anybody help me in this matter?
Thanks
Megana
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
megana,
Javaranch has a naming policy which is strictly enforced. Please change your display name to conform with this policy if you wish to keep posting here. Thanks.
 
Prashanth menon
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi megana
The problem if there is no exception is not with the code. The file has been saved. The file is saved to a default directory that depends on the container you use. If you use Tomcat4.0 on Winnt this file is saved in the System32 folder.
For Jrun3.0 the file is written to Jrunroot/servers/default folder
Actually you can write a servlet that reads from the file and the returns image.
I had posted on this earlier.
regards
Prashanth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic