commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Victor Kam" <>
Subject Fileupload Help! Urgent!
Date Sun, 15 Jun 2003 06:51:42 GMT
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
<html:form action="/upload" method="POST" enctype="multipart/form-data">
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!

View raw message