commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Maccariella <>
Subject Re: Fileupload Help! Urgent!
Date Mon, 16 Jun 2003 13:06:37 GMT
I have a similar class called UploadAction using FileUpload.  It is defined as:
package upload;
import org.apache.commons.fileupload.*;
import org.apache.struts.action.*;
import java.util.*;
import javax.servlet.http.*;
public class UploadAction
  extends Action
    public ActionForward execute( 
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response )
      throws Exception
            FileUpload upload = new FileUpload(  );
            upload.setSizeMax( 100000000 );
            upload.setRepositoryPath( "c:\\temp\\" );
            List list = upload.parseRequest( request );
            for ( Iterator i = list.iterator(  ); i.hasNext(  ); )
                FileItem fileItem = ( FileItem )  );
                if ( fileItem.getContentType(  ) != null )
                        // ... do something with the fileItem ...
                    catch ( IOException ioe )
                        ioe.printStackTrace(  );
            return mapping.findForward( "success" );
        catch ( Throwable t )
            t.printStackTrace(  );
            return mapping.findForward( "failure" );
In my JSP, my <form> tag looks like this:
<form action="<%= request.getContextPath() %>/action/upload" enctype="multipart/form-data"
Hope this helps,

Victor Kam <> wrote:
Hi all,

I am trying to use the commons file upload package to handle file upload 
request in my application, yet I seemed to always get an empty List from the 
parseRequest function of the DiskFileUpload class. I am handling file 
uploads in a class name uploadAction, which extends from 
org.apache.struts.action.Action class. I read from a previous thread 
that someone is having the similar problem, but maybe because I am new to 
struts, etc. I do not seem to understand what to do about the situation. Is 
it inappropriate to implement the fileupload in a Action subclass!? If so, 
how should I go about solving the problem using strut's ActionServlet/Action 
model and file upload packages together!? The following is my uploadAction 

package com.bat.upload.action;
import com.bat.common.action.BaseAction;
import com.bat.upload.form.UploadForm;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import java.util.List;
import java.util.Iterator;
public class UploadAction extends BaseAction
public UploadAction()
public ActionForward perform(ActionMapping mapping, ActionForm f, 
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
System.out.println("UploadAction.perform entered.");
UploadForm form = (UploadForm) f;
if(form == null)
System.out.println("NULL form");
if(form.getUploadFile() != null)
System.out.println("filename : %" + form.getUploadFile().getFileName() + 
if(form.getUploadFile() != null)
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
// maximum size that will be stored in memory
// the location for saving data that is larger than getSizeThreshold()
List fileItems = fu.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
System.out.println("num of fileItems: " + fileItems.size());
Iterator i = fileItems.iterator();
String comment = ((FileItem);
System.out.println("comment: " + comment);
FileItem fi = (FileItem);
// filename on the client
String fileName = fi.getName();
System.out.println("file to write: " + 
getServlet().getServletContext().getRealPath("/SOP")+ "/" + fileName + "%");
// write the file
File tmpFile = new 
File(getServlet().getServletContext().getRealPath("/SOP")+ "/" + fileName);
catch(FileUploadException fue)
throw new ServletException("FileUploadError: " + fue.getMessage(), 
catch(Exception e)
throw new ServletException("FileWriteError", e);
ActionForward forwardResult = new ActionForward(mapping.getInput());
return forwardResult;

My form is declared as

and the isMultipartContent() also returns true, problem is I always get an 
empty fileItem list returned from parseRequest(). Any suggestions to this 
problem would be strongly appreciated.

Thanks alot in advance,


Get 10Mb extra storage for MSN Hotmail. Subscribe Now!

To unsubscribe, e-mail:
For additional commands, e-mail:


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message