Re: FileUpload Question

2007-03-31 Thread Jochen Wiedmann

On 3/31/07, Thomas Vanck <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Fileupload question

2006-06-23 Thread Nima

Bah, this has left me freaking clueless.I will try the same code at work to
see if I have any luck on another computer. If any of you guys have used the
Fileupload API with Eclipse WTP/Tomcat 5.5 (not in combination with a web
framework such as WebWork or Tapestry), please let me know how you got it to
work! And thanks for taking the time Martin :)

On 6/23/06, Martin Cooper <[EMAIL PROTECTED]> wrote:


On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:
>
> I did ignore setting the threshold and max file size before, but that
> didn't
> result in any luck either... basically I had the following in my method
> body
>
> DiskFileItemFactory factory = new DiskFileItemFactory();
> factory.setRepository(new File("C:/"));
> ServletFileUpload upload = new ServletFileUpload(factory);
>
> try {
> List items = upload.parseRequest(request);
>
> Iterator iter = items.iterator();
> while(iter.hasNext()) {
> FileItem item = (FileItem) iter.next();
> item.write(new File("test.jpg"));
> }
>
> } catch(
>
> The Eclipse debugger shows these variable values for the upload
> object:
> upload= ServletFileUpload  (id=1454)
> fileItemFactory= DiskFileItemFactory  (id=1443)
> headerEncoding= null
> sizeMax= -1
>
> I also tried setting the repository location to the servlet path (and
not
> setting it at all) but no dice. I am running Eclipse WTP alongside
Tomcat
> 5.5. My gut feeling is that something must be configured wrong - what
are
> the most basic steps that must be taken to get a single file uploaded
via
> the Commons Fileupload? (the documentation is rather scarce for this
API).


The user guide shows you exactly the minimum set of steps you need to get
going. It's in the section titled "The simplest case". ;-)

If that isn't working, then I'd look to the environment, rather than your
code. Tomcat won't automatically parse multipart requests, but if you have
a
framework in place (e.g. Struts), then that might be doing it. The issue
here is that the request can be parsed only once, so if anything else gets
to it before you do, then there will be nothing left for you to parse. And
if that also isn't the case, then you may need to look at the stream
itself,
and add a test to your code to check for a multipart request.

--
Martin Cooper


Thanks again!
>
> On 6/23/06, Martin Cooper <[EMAIL PROTECTED]> wrote:
> >
> > On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:
> > >
> > > Hey all
> > >
> > > I am trying to handle a JSP file upload with the Commons Fileupload
> api
> > > but
> > > to no avail. The problem is that I never manage to parse the request
> > > coming
> > > from my JSP
> > > to my Servlet. Any ideas on what I might be doing wrong? There are
no
> > > exceptions thrown and I have the commons-io.jar in my web-inf/lib so
I
> > > should be all set. I've been dealing with this issue for over two
days
> > > now,
> > > any form of input is helpful!
> >
> >
> > A couple of things:
> >
> > 1) What container are you using? Are you sure the container itself has
> not
> > already parsed the input before you try to do so?
> >
> > 2) You are setting the threshold and the max to the same value, so
> nothing
> > will ever be stored on disk. And then you are trying to configure a
> > repository on disk, which would never be used. Not sure what you're
> trying
> > to accomplish, but whatever it is, this combination doesn't make
sense.
> >
> > 3) You are passing a bogus value to setRepository. If you want it to
use
> a
> > specific directory, pass that. If you don't, don't call the method.
> >
> > --
> > Martin Cooper
> >
> >
> > Here's the Servlet method
> > >
> > > protected void doPost(HttpServletRequest request,
> > > HttpServletResponse response) throws ServletException,
> > > IOException {
> > >
> > > PrintWriter out = response.getWriter();
> > >
> > > DiskFileItemFactory factory = new DiskFileItemFactory();
> > > factory.setSizeThreshold(2);
> > > factory.setRepository(new File(""));
> > > ServletFileUpload upload = new ServletFileUpload(factory);
> > > upload.setSizeMax(2);
> > > File file = new File("/");
> > >
> > > try {
> > > List items = upload.parseRequest(request); //List never
> > > populates :(
> > >
> > > Iterator iter = items.iterator();
> > > while(iter.hasNext()) {
> > > FileItem item = (FileItem) iter.next();
> > > item.write(file);
> > > }
> > >
> > > } catch(Exception e) {
> > > e.printStackTrace();
> > > }
> > >
> > > }
> > >
> > > And the JSP HTML
> > >
> > >  > > enctype="multipart/form-data">
> > > 
> > > 
> > > 
> > >
> > >
> >
> >
>
>




Re: Fileupload question

2006-06-23 Thread Martin Cooper

On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:


I did ignore setting the threshold and max file size before, but that
didn't
result in any luck either... basically I had the following in my method
body

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("C:/"));
ServletFileUpload upload = new ServletFileUpload(factory);

try {
List items = upload.parseRequest(request);

Iterator iter = items.iterator();
while(iter.hasNext()) {
FileItem item = (FileItem) iter.next();
item.write(new File("test.jpg"));
}

} catch(

The Eclipse debugger shows these variable values for the upload
object:
upload= ServletFileUpload  (id=1454)
fileItemFactory= DiskFileItemFactory  (id=1443)
headerEncoding= null
sizeMax= -1

I also tried setting the repository location to the servlet path (and not
setting it at all) but no dice. I am running Eclipse WTP alongside Tomcat
5.5. My gut feeling is that something must be configured wrong - what are
the most basic steps that must be taken to get a single file uploaded via
the Commons Fileupload? (the documentation is rather scarce for this API).



The user guide shows you exactly the minimum set of steps you need to get
going. It's in the section titled "The simplest case". ;-)

If that isn't working, then I'd look to the environment, rather than your
code. Tomcat won't automatically parse multipart requests, but if you have a
framework in place (e.g. Struts), then that might be doing it. The issue
here is that the request can be parsed only once, so if anything else gets
to it before you do, then there will be nothing left for you to parse. And
if that also isn't the case, then you may need to look at the stream itself,
and add a test to your code to check for a multipart request.

--
Martin Cooper


Thanks again!


On 6/23/06, Martin Cooper <[EMAIL PROTECTED]> wrote:
>
> On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:
> >
> > Hey all
> >
> > I am trying to handle a JSP file upload with the Commons Fileupload
api
> > but
> > to no avail. The problem is that I never manage to parse the request
> > coming
> > from my JSP
> > to my Servlet. Any ideas on what I might be doing wrong? There are no
> > exceptions thrown and I have the commons-io.jar in my web-inf/lib so I
> > should be all set. I've been dealing with this issue for over two days
> > now,
> > any form of input is helpful!
>
>
> A couple of things:
>
> 1) What container are you using? Are you sure the container itself has
not
> already parsed the input before you try to do so?
>
> 2) You are setting the threshold and the max to the same value, so
nothing
> will ever be stored on disk. And then you are trying to configure a
> repository on disk, which would never be used. Not sure what you're
trying
> to accomplish, but whatever it is, this combination doesn't make sense.
>
> 3) You are passing a bogus value to setRepository. If you want it to use
a
> specific directory, pass that. If you don't, don't call the method.
>
> --
> Martin Cooper
>
>
> Here's the Servlet method
> >
> > protected void doPost(HttpServletRequest request,
> > HttpServletResponse response) throws ServletException,
> > IOException {
> >
> > PrintWriter out = response.getWriter();
> >
> > DiskFileItemFactory factory = new DiskFileItemFactory();
> > factory.setSizeThreshold(2);
> > factory.setRepository(new File(""));
> > ServletFileUpload upload = new ServletFileUpload(factory);
> > upload.setSizeMax(2);
> > File file = new File("/");
> >
> > try {
> > List items = upload.parseRequest(request); //List never
> > populates :(
> >
> > Iterator iter = items.iterator();
> > while(iter.hasNext()) {
> > FileItem item = (FileItem) iter.next();
> > item.write(file);
> > }
> >
> > } catch(Exception e) {
> > e.printStackTrace();
> > }
> >
> > }
> >
> > And the JSP HTML
> >
> >  > enctype="multipart/form-data">
> > 
> > 
> > 
> >
> >
>
>




Re: Fileupload question

2006-06-23 Thread Nima

I did ignore setting the threshold and max file size before, but that didn't
result in any luck either... basically I had the following in my method body

   DiskFileItemFactory factory = new DiskFileItemFactory();
   factory.setRepository(new File("C:/"));
   ServletFileUpload upload = new ServletFileUpload(factory);

   try {
   List items = upload.parseRequest(request);

   Iterator iter = items.iterator();
   while(iter.hasNext()) {
   FileItem item = (FileItem) iter.next();
   item.write(new File("test.jpg"));
   }

   } catch(

   The Eclipse debugger shows these variable values for the upload object:
   upload= ServletFileUpload  (id=1454)
   fileItemFactory= DiskFileItemFactory  (id=1443)
   headerEncoding= null
   sizeMax= -1

I also tried setting the repository location to the servlet path (and not
setting it at all) but no dice. I am running Eclipse WTP alongside Tomcat
5.5. My gut feeling is that something must be configured wrong - what are
the most basic steps that must be taken to get a single file uploaded via
the Commons Fileupload? (the documentation is rather scarce for this API).

Thanks again!

On 6/23/06, Martin Cooper <[EMAIL PROTECTED]> wrote:


On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:
>
> Hey all
>
> I am trying to handle a JSP file upload with the Commons Fileupload api
> but
> to no avail. The problem is that I never manage to parse the request
> coming
> from my JSP
> to my Servlet. Any ideas on what I might be doing wrong? There are no
> exceptions thrown and I have the commons-io.jar in my web-inf/lib so I
> should be all set. I've been dealing with this issue for over two days
> now,
> any form of input is helpful!


A couple of things:

1) What container are you using? Are you sure the container itself has not
already parsed the input before you try to do so?

2) You are setting the threshold and the max to the same value, so nothing
will ever be stored on disk. And then you are trying to configure a
repository on disk, which would never be used. Not sure what you're trying
to accomplish, but whatever it is, this combination doesn't make sense.

3) You are passing a bogus value to setRepository. If you want it to use a
specific directory, pass that. If you don't, don't call the method.

--
Martin Cooper


Here's the Servlet method
>
> protected void doPost(HttpServletRequest request,
> HttpServletResponse response) throws ServletException,
> IOException {
>
> PrintWriter out = response.getWriter();
>
> DiskFileItemFactory factory = new DiskFileItemFactory();
> factory.setSizeThreshold(2);
> factory.setRepository(new File(""));
> ServletFileUpload upload = new ServletFileUpload(factory);
> upload.setSizeMax(2);
> File file = new File("/");
>
> try {
> List items = upload.parseRequest(request); //List never
> populates :(
>
> Iterator iter = items.iterator();
> while(iter.hasNext()) {
> FileItem item = (FileItem) iter.next();
> item.write(file);
> }
>
> } catch(Exception e) {
> e.printStackTrace();
> }
>
> }
>
> And the JSP HTML
>
>  enctype="multipart/form-data">
> 
> 
> 
>
>




Re: Fileupload question

2006-06-23 Thread Martin Cooper

On 6/23/06, Nima <[EMAIL PROTECTED]> wrote:


Hey all

I am trying to handle a JSP file upload with the Commons Fileupload api
but
to no avail. The problem is that I never manage to parse the request
coming
from my JSP
to my Servlet. Any ideas on what I might be doing wrong? There are no
exceptions thrown and I have the commons-io.jar in my web-inf/lib so I
should be all set. I've been dealing with this issue for over two days
now,
any form of input is helpful!



A couple of things:

1) What container are you using? Are you sure the container itself has not
already parsed the input before you try to do so?

2) You are setting the threshold and the max to the same value, so nothing
will ever be stored on disk. And then you are trying to configure a
repository on disk, which would never be used. Not sure what you're trying
to accomplish, but whatever it is, this combination doesn't make sense.

3) You are passing a bogus value to setRepository. If you want it to use a
specific directory, pass that. If you don't, don't call the method.

--
Martin Cooper


Here's the Servlet method


protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

PrintWriter out = response.getWriter();

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(2);
factory.setRepository(new File(""));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(2);
File file = new File("/");

try {
List items = upload.parseRequest(request); //List never
populates :(

Iterator iter = items.iterator();
while(iter.hasNext()) {
FileItem item = (FileItem) iter.next();
item.write(file);
}

} catch(Exception e) {
e.printStackTrace();
}

}

And the JSP HTML









Fileupload question

2006-06-23 Thread Nima

Hey all

I am trying to handle a JSP file upload with the Commons Fileupload api but
to no avail. The problem is that I never manage to parse the request coming
from my JSP
to my Servlet. Any ideas on what I might be doing wrong? There are no
exceptions thrown and I have the commons-io.jar in my web-inf/lib so I
should be all set. I've been dealing with this issue for over two days now,
any form of input is helpful!

Here's the Servlet method

protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException,
IOException {

   PrintWriter out = response.getWriter();

   DiskFileItemFactory factory = new DiskFileItemFactory();
   factory.setSizeThreshold(2);
   factory.setRepository(new File(""));
   ServletFileUpload upload = new ServletFileUpload(factory);
   upload.setSizeMax(2);
   File file = new File("/");

   try {
   List items = upload.parseRequest(request); //List never
populates :(

   Iterator iter = items.iterator();
   while(iter.hasNext()) {
   FileItem item = (FileItem) iter.next();
   item.write(file);
   }

   } catch(Exception e) {
   e.printStackTrace();
   }

   }

And the JSP HTML