Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem in sending image from applet to servlet

843841Nov 24 2007 — edited Nov 25 2007
dear friends,

i have a need to send an image from applet to servlet via HttpConnection and getting back that image from applet.
i am struggling with this sice many hours and got tired by searching any post that would help me but haven't got yet.

i tried using this code but it dosent make any execution sit right. i got NPE at ImageIcon.getDescription() line;

at applet side
      jf.setContentPane(getJContentPane());
                 FileDialog fd=new FileDialog(jf,"hi");
                 
                 fd.setMode(FileDialog.LOAD);
                 fd.setVisible(true);    
                 
                 v=new Vector();
                 
                 try{                                                 
                             
                           FileInputStream fis=new FileInputStream(new File(fd.getDirectory()+fd.getFile()));       
                           byte[] imgbuffer=new byte[fis.available()];
                           fis.read(imgbuffer);
                           
                           ImageIcon imgdata=new ImageIcon(imgbuffer);
                           v.add(0,imgicon);
                          
                            String strwp ="/UASProject/Storeimage";               
                            URL servletURL = new URL(getCodeBase(),strwp);              
        
                            HttpURLConnection servletCon = (HttpURLConnection)servletURL.openConnection();        
      
                            servletCon.setDoInput(true);  
                            servletCon.setDoOutput(true);
       
                            servletCon.setUseCaches(false);
                            servletCon.setDefaultUseCaches(false);    
        
                            servletCon.setRequestMethod("POST");      
                            servletCon.setRequestProperty("Content-Type", "application/octet-stream");    
                        
                            servletCon.connect();             
                            ObjectOutputStream oboutStream = new ObjectOutputStream(servletCon.getOutputStream());                      
                            oboutStream.writeObject(v); 
                            v.remove(0);
                            
                            oboutStream.flush();       
                            oboutStream.close();   
                            
                            //read back from servlet
               
                            ObjectInputStream inputStream = new ObjectInputStream(servletCon.getInputStream());
       
                             v= (Vector)inputStream.readObject();                      
                             imgicon=(ImageIcon)v.get(1);
                             showimg.setIcon(imgicon);
                             this.getContentPane().validate();
                             this.validate();   
                            inputStream.close();                                                         
                         
                         //  repaint();
                           
                 }catch(Exception e){e.printStackTrace();}  
and this is at servlet side
        try {        
                     Vector v=new Vector();                     
                  
                     ObjectInputStream inputFromjsp = new ObjectInputStream(request.getInputStream());                                       
                      v = (Vector)inputFromjsp.readObject();                                                                                                           
                      imgicon=(ImageIcon)v.get(0);                      
                      inputFromjsp.close();             
                      
                      System.out.println(imgicon.getDescription());                                       
                      v.remove(0);
                      v.add(1,imgicon);

//sending back to applet

                       response.setContentType("application/octet-stream");
                      ObjectOutputStream oboutstream=new ObjectOutputStream(response.getOutputStream());             
                      oboutstream.writeObject(v);
                      oboutstream.flush();
                      oboutstream.close();
                 
                  
               } catch (Exception e) {e.printStackTrace();}  
i really need your help. please let me out of this headche

thanks

Edited by: san_4u on Nov 24, 2007 1:00 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2007
Added on Nov 24 2007
18 comments
361 views