You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Will Stranathan <ws...@hotmail.com> on 2003/06/15 16:04:01 UTC
Re: Fileupload Help! Urgent!
The request stream for a multipart/form-data can only be read once - by the
time Struts decides what Action to execute, goes through setting up the Form
and validating, it's actually already read the request. Use a formBean that
has accessors/mutators for org.apache.struts.upload.FormFile's, and pass the
formBean to your action.
See the struts-upload webapp for a very good example.
The short answer is - you don't HAVE to do all the junk below when you're
using Struts - the smart developers for Struts did all that junk for you so
you can just use your file directly.
Will
>From: "Victor Kam" <vc...@hotmail.com>
>Reply-To: "Jakarta Commons Users List" <co...@jakarta.apache.org>
>To: commons-user@jakarta.apache.org
>Subject: Fileupload Help! Urgent!
>Date: Sun, 15 Jun 2003 14:51:42 +0800
>
>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
><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: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail