struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abhiram <abhi_...@yahoo.com>
Subject Re: file upload exception
Date Fri, 14 Sep 2007 18:38:05 GMT
hi!! i dont know about struts 2, but i tried uploading images to the server using Struts 1.2,and
it works just fine..i dont think we get the uploaded file as it is.. wat u said is true..
we get a temp file at different locations for each web container...basically watever files
are uploaded, are obtained as objects of class FormFile. we have a method called FormFile.getInputStream()..i
think the rest is understood..get the InputStream,name the o/p file appropriately using FormFile.getContentType()
,write to the o/p file . sorry that iam not able to give you the jsp code in the tags format
that you wrote..i dont know it.if u have multiple files to handle, get them as a FormFile
array, for each element,verify content type and call the method where the  file saving logic
is written

here is the code

in the jsp,

<html:form action="/UploadAction.do?" 
               method="post" enctype="multipart/form-data">
      File 1 <html:file property="myFile1"/> <br>
     <html:submit>Upload Files</html:submit>
</html:form>
-------------------------------------------------------------------
in the form bean,
public class UploadForm extends ActionForm{
    
    FormFile myFile1;
   public FormFile getMyFile1() {
        return myFile1;
    }
    
    public void setMyFile1(FormFile myFile1) {
        this.myFile1 = myFile1;
    }
}
---------------------------------------------------------------------------
in the execute method of Action,

UploadForm uForm = (UploadForm) form;
    FormFile f = uForm.getMyFile1();
    InputStream is = f.getInputStream();
    FileOutputStream fout = new FileOutputStream("putSomePathHere");
   int i=0;
             do{
                 i = is.read();

                 if(i != -1) fout.write(i);
               } while(i != -1);
             is.close();
             fout.close();
             f.destroy();
             System.out.println("Temp File removed");
         }




      abhiram




       
---------------------------------
 Get the freedom to save as many mails as you wish. Click here to know how.
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message