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/[EMAIL PROTECTED]/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
<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,
Victor
_________________________________________________________________
Get 10Mb extra storage for MSN Hotmail. Subscribe Now! http://join.msn.com/?pgmarket=en-hk
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
