commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Wiedmann" <jochen.wiedm...@gmail.com>
Subject Re: FileUpload Question
Date Sat, 31 Mar 2007 16:24:27 GMT
On 3/31/07, Thomas [ELIDED] <tom[ELIDED]@gmail.com> wrote:
> Hi !
>
> I'm using FileUpload in my Java/Tomcat project. I've a problem, which
> is I think mentioned in the FAQ, but no solution is offered there.
> Searching the web and asking on boards didn't yield any results.
>
> I've written the following code:
> package requestHandler;
>
> import javax.servlet.http.*;
> import org.apache.commons.fileupload.*;
> import org.apache.commons.fileupload.servlet.*;
> import org.apache.commons.fileupload.disk.*;
> import java.io.*;
> import java.util.*;
>
> public class PictureHandler extends HttpServlet {
>
>         static final long serialVersionUID = 6;
>
>         public void doPost( HttpServletRequest request , HttpServletResponse
> response ) {
>
>                 FileItemFactory ff = new DiskFileItemFactory();
>                 ServletFileUpload upload = new ServletFileUpload( ff );
>                 if( ServletFileUpload.isMultipartContent( request ) ) {
>                         try {
>                                 List filelist = upload.parseRequest( request );
>                                 System.out.println( "filelist.size():" + filelist.size()
);
>                                 Iterator it = filelist.iterator();
>                                 while( it.hasNext() ) {
>                                         FileItem pic = (FileItem) it.next();
>                                         String filename = pic.getName();
>                                         pic.write( new File( "/User/toom/Desktop/mypic/"
, filename ) );
>                                         System.out.println("Datei geschrieben...");
>                                 }
>                         } catch ( Exception e ) { e.printStackTrace(); return; }
>                 }
>
>                 try { request.getRequestDispatcher("upload_pictures.jsp").forward(
> request , response ); return; } catch (Exception e) {
> e.printStackTrace(); }
>         }
> }
>
> Now, when calling this servlet with a multipart/data form request, my
> doPost tells me that filelist.size() has length 0.  So I expect some
> preprocessing is done with this request. But how can I tell Tomcat,
> not to preprocess this data ?

Hi,

first of all, please be so kind to direkt such questions to
commons-dev@jakarta.apache.org or to a similar mailing list and not to
me privately.

Second, I assume that you are referring to version 1.2 of
commons-fileupload. If you have a look at the code of parseRequest(),
then you'll find the following:


            FileItemIterator iter = getItemIterator(ctx);
            List items = new ArrayList();
            ...
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                FileItem fileItem = fac.createItem(item.getFieldName(),
                        item.getContentType(), item.isFormField(),
                        item.getName());
                ...
                items.add(fileItem);
            }
            return items;

In other words, what the method returns is just a simple ArrayList,
without any magic. Reformulated again, filelist.size() is zero, if,
and only if, the Iterator returns nothing. If that's not the case then
you have detected an error in java.util.ArrayList, which I doubt.

Jochen

-- 
My cats know that I am a loser who goes out for hunting every day
without ever returning as much as a single mouse. Fortunately, I've
got a wife who's a real champ: She leaves the house and returns within
half an hour, carrying whole bags full of meal.

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


Mime
View raw message