commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Maccariella <tmacc...@yahoo.com>
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.io.*;
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
    {
        try
        {
            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 ) i.next(  );
                if ( fileItem.getContentType(  ) != null )
                {
                    try
                    {
                        // ... 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"
method="post">
 
Hope this helps,
Tom

Victor Kam <vchkam@hotmail.com> 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 
(http://www.mail-archive.com/commons-user@jakarta.apache.org/msg02231.html) 
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 
class.

package com.bat.upload.action;
import com.bat.common.action.BaseAction;
import com.bat.upload.form.UploadForm;
import java.io.IOException;
import java.io.PrintStream;
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;
import java.io.File;
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");
else
{
if(form.getUploadFile() != null)
System.out.println("filename : %" + form.getUploadFile().getFileName() + 
"%");
}
if(form.getUploadFile() != null)
{
try
{
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");
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)i.next()).getString();
System.out.println("comment: " + comment);
FileItem fi = (FileItem)i.next();
// 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);
fi.write(tmpFile);
}
catch(FileUploadException fue)
{
throw new ServletException("FileUploadError: " + fue.getMessage(), 
fue);
}
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,

Victor

_________________________________________________________________
Get 10Mb extra storage for MSN Hotmail. Subscribe Now! 
http://join.msn.com/?pgmarket=en-hk


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


 



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