GeeCON Prague 2014*
The moose likes Applets and the fly likes Writing Image from Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "Writing Image from Applet" Watch "Writing Image from Applet" New topic
Author

Writing Image from Applet

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi,

I have a small applet in that am viewing a image using

URL url = new URL(getCodeBase(), "sample.jpg");
BufferedImage image = ImageIO.read(url);

After that am writing a new image using

URL url1 = new URL(getCodeBase(), "new.jpg");
ImageOutputStream ios = ImageIO.createImageOutputStream(url1);
ImageIO.write(image, type, ios);

But it's not working. Please assist me how to write image from Applet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
A static image served by a web server is a read-only URL. If you want to save images, then you need to send the image data to an active server-side component (like a servlet) over HTTP. That component can then save the image data on the server.


Ping & DNS - my free Android networking tools app
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Thanks a lot..I will try that.. But in case if it a stanalone Applet.. Then how to save an image to the server machine..
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Ulf,

Now it's working fine. But please assit me how to save an image now. My Application is struts and the applet class is out of WEB-INF directory, Then how can I pass the image from applet to jsp or servlet.

Please assit me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
Do you mean a stand alone application? (Applets are running inside of web browsers.) They can use any means supported by the server - HTTP upload, WebDAV, FTP, shared directory, etc. - depends on what's available.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
Then how can I pass the image from applet to jsp or servlet.

The applet can make an HTTP POST to a servlet, and in that call send all data needed to reconstitute the image file.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
But in my it's a struts application. Also my applet class are out of WEB-INF Folder.. So Don't know how to call the struts handler.. Please help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
Yes, there are two parts to it - the applet would contain code that makes the HTTP call, and the web app would contain code (a servlet or a Struts action) that receives the binary data sent by the image.

I suggest to start by writing an applet that makes a successful HTTP call to a servlet (or Struts action); you can worry about sending the actual image data once you have that.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Am trying to access a action from the applet using

URL urlServlet = new URL(getCodeBase(), "Dummy.do");

getCodeBase() - Is returning Nullponiterexception.


Any suggestion please.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
Are you running the applet off a server? Meaning, does the URL in the browser start with "http"? Otherwise there's no code base that could meaningfully be returned.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
After placing URL urlServlet = new URL(getCodeBase(), "Dummy.do"); in the init() it's working..

Anyway to change the getcodebase() value ? Because am placing my applets in a different folder am getting that folder name too appended in the url

Thanks..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
If the servlet has a different URL (which is quite likely), you can pass that URL to the applet through an applet parameter. That's a good idea anyway instead of putting any part of the servlet URL in the applet code.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Thats Great !!... I am doing that now

But am getting java.io.NotSerializableException: java.awt.image.BufferedImage while writing from applet




Can I try this or any other good solution ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
RMI wouldn't work, but you could use the getRGB method mentioned there.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
My Applet is not hitting my action. What goes wrong please help ?


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
You need to handle the response from the servlet, otherwise the request won't even get sent.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Great Ulf.... After adding below code.. Hit goes to ApplicationController(Struts Controller) but not going to my Struts Action Handler... Any guess

Please share your thoughts


Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
After removing the waitpage from struts-config it works..!!.. why it's not working if the please wait page is enabled..
 
GeeCON Prague 2014
 
subject: Writing Image from Applet