Current Position:Home > Writing Image files from Applet to Servlet

Writing Image files from Applet to Servlet

Update:10-11Source: network consolidation
Advertisement
How to write ImageFiles from an Applet to a Servlet?
I draw a image on a JComponent in Applet and wanted to save the image file in the server.
I am unable to write an Image Object to the Servlet as the BufferedImage class is not serialized.
I tried writing the image to the Servlet output stream and tried reading the data in the servlet and writing the data to a jpg file.
But the files is not being written as proper jpeg file.
Any help would be great.
Thanks,
Sridhar.

The Best Answer

Advertisement
I get the following exception below when i try to write a Serialized object from an applet to the servlet.
I copied the serialized class jar file to the tomcat webserver lib folder.
The serialized class has a BufferedImage object.
Serialized Class Code
public class SerializedImage implements Serializable{
     private BufferedImage im = null;
     public SerializedImage() {
          super();
     public BufferedImage getSerializedObject() {
          return im;
     public void setSerializedObject((BufferedImage im) {
          this.im = im;
     private BufferedImage fromByteArray(byte[] imagebytes) {
          try {
               if (imagebytes != null && (imagebytes.length > 0)) {
                    BufferedImage im = ImageIO.read(new ByteArrayInputStream(imagebytes));
                    return im;
               return null;
          } catch (IOException e) {
               throw new IllegalArgumentException(e.toString());
     private byte[] toByteArray(BufferedImage o) {
          if(o != null) {
               BufferedImage image = (BufferedImage)o;
               ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
               try {
                    ImageIO.write(image, "jpg", baos);
               } catch (IOException e) {
                    throw new IllegalStateException(e.toString());
               byte[] b = baos.toByteArray();
               return b;
          return new byte[0];
     private void writeObject(java.io.ObjectOutputStream out)
     throws IOException {
          byte[] b = toByteArray(im);
          out.writeInt(b.length);
          out.write(b);
     private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException {
          int length = in.readInt();
          byte[] b = new byte[length];
          in.read(b);
          im = fromByteArray(b);
Exception :
java.io.StreamCorruptedException: unexpected block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1288)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:18
45)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
Is there any thing that i am missing or doing wrong?
Thanks,
Sridhar.
  • Writing Image files from Applet to Servlet Update:10-11

    How to write ImageFiles from an Applet to a Servlet? I draw a image on a JComponent in Applet and wanted to save the image file in the server. I am unable to write an Image Object to the Servlet as the BufferedImage class is not serialized. I tried w

  • Sending a file from Applet to servlet HELP me Please Update:10-11

    Sorry, i have the problem this is my code Applet & Servlet but it seems working asynchronously if you have some ideas please reply me i send bytes on outputstream but the inputstream of servlet receive nothing bytes but write my system.out.print on s

  • Uploading large files from applet to servlet throws out of memory error Update:10-11

    I have a java applet that needs to upload files from a client machine to a web server using a servlet. the problem i am having is that in the current scheme, files larger than 17-20MB throw an out of memory error. is there any way we can get around t

  • Writing to file in applet Update:10-11

    Hi guys! I have an applet and i want it to write to a file, but i know that there is some problem with security, but i really need a way to save some information aomewhere during the program an dit must be an applet. is there away? thanx. here's the

  • Writing local files from applet Update:11-30

    Hi, I want to write an image file from an applet (appletviewer) to the local file system. Does anyone have any idea how I can do it? I tried using the FilePermission class but failed in doing so. Thanx for any help in advanceI need to do this too. Sp

  • Applet: writing to file Update:11-30

    hi there. i need serious help on using an applet and writing the user response into a text file. issit possible in the first place? cos i read at a lot of sources saying applet cant write to file? any help is much appreciated thx!Where do you plane o

  • Passing a file as parameter from applet to servlet Update:11-30

    hi i have just started to use servlets. i have developed applet gui interface. when i click the button i want a file "abc.txt" to be send to the servlet. i am doing like this: URL serv_url = new URL(null,"http://host:port/xyz/servlet/TestSe

  • Sending different types of data from applet to servlet Update:11-30

    Hi, I am writing an applet that uploads a file to a servlet. I can send the file to the servlet just fine. Applet Side 1. open a URLConnection 2. open a DataOutputStream 3. write the file to the DataOutputStream Servlet Side: 1. open a ServletInputSt

  • EXPORT XFA error while writing XFA files Update:10-11

    Hello Experts, We are facing an error while exporting to PDF from BEx report. We are on EP7 SP14. BI on SP16. After the report is displayed and Export to PDF button is clicked, a new window opens which shows following: 1. Error: Message: No message w

  • File does not exist: /servlet/f60servlet Update:10-11

    Hi, We have a Developer 2000 application which is running on 9ias. Till last week the UAT system was working ok. Since i re-started, it stopped working. The apache error log displays the following error messages: [Tue Jan 25 15:53:08 2005] [error] [c