Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts FileUpload 2

Status
Not open for further replies.

samit700

Programmer
Sep 23, 2003
52
0
0
US
Does anyone know where I can find information on file upload using Struts. I tried to write a simple action that will upload a file and process it but it is failing...

sample code wil be great
 
How far are you on this. what error are you getting?

Here is an example:

In your strut-config, you define the form-bean and form action like this:
Code:
...
</form-beans>
   <form-bean name="uploadForm" type="yourpackage.UploadForm" />
</form-beans>
<action-mappings>
   <action  attribute="uploadForm"
            input="/upload.jsp"
            name="uploadForm"
            path="/upload"
            scope="request"
            type="yourpackage.UploadSubmit">
      <forward name="success" path="/uploadComplete.jsp" />
   </action>
</action-mappings>
...
In your form page 'upload.jsp', you define the upload form like:
Code:
<html:form action="/uploadSubmit" enctype="multipart/form-data">
   <html:file property="file"/>
</html:form>


In your Form class
Code:
package yourpackage
public class UploadForm extends ActionForm {
   private FormFile file;

   public FormFile getFile() {
      return file;
   }

   public void setFile(FormFile file) {
      this.file = file;
   }
}

In your upload action class
Code:
public class UploadSubmit extends Action {

   public ActionForward execute(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
                 throws Exception {

        UploadForm uploadForm = (UploadForm) form;
        Hashtable files = uploadForm.getMultipartRequestHandler().getFileElements();
        FormFile theFile = (FormFile) files.get("file");

        InputStream stream = theFile.getInputStream();

        File saveToFile = new File(theFile.getFileName());

        OutputStream bos = new FileOutputStream(saveToFile);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
           bos.write(buffer, 0, bytesRead);
        }
        bos.close();

        stream.close();
        return mapping.findForward("success");
   }
}

hope this help
 
Hi,

Thanks for your reply. I was doing exactly the same thing. But I was using InputStreamReader to read characters. For some reasons it was going into an infinte loop. THe boundary condition is the same in both the cases (...!=-1).
If I use simply inputstream returned by file.getInputStream(), it works fine. I wonder why its not working if I wrap the input stream into an inputstreamreader ...

samit
 
I don't see any reason why InputStreamReader would behaviour differently, except it reads in characters instead of byte. So depending on character encoding of the stream, and character being read, a single character may result in more than one byte.

May be if you can show us the the code how your read the wrapped input steam and see if we can find anything wrong with it.
 
I replaced the code so dont have it anymore. But it was something like this:
Code:
...
InputStream stream = theFile.getInputStream();
//assuming text file being uploaded and default platform
//encoding
InputStreamReader reader = new InputStreamReader(stream);
while (true) {
 char c = (char) reader.read();
 if (c==-1) {
   break;
 }
 System.out.print(c);
}
...

The above code went into an infinite loop.

Samit
 
The problem is not because of InputStreamReader. It is due to the fact that read() return int (32 bits), but is casted to char (16 bits). After an 'int' casted to 'char', it losts the most significant 16 bits. As a result -1 becomes 65535.

The code should be:
Code:
while (true) {
 int c = reader.read();
 if (c==-1) {
   break;
 }
 System.out.print((char)c);
}
 
Forgot to mention, there is no -1 in char.
 
Thanks byam! Your example is exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top