You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Nima <de...@gmail.com> on 2006/06/23 16:56:13 UTC

Fileupload question

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(20000);
        factory.setRepository(new File(""));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(20000);
        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

<form action="FrontController" method="POST" enctype="multipart/form-data">
        <input type="file" value="filename" />
        <input type="submit" value="Submit" />
</form>

Re: Fileupload question

Posted by Nima <de...@gmail.com>.
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 <ma...@apache.org> wrote:
>
> On 6/23/06, Nima <de...@gmail.com> 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 <ma...@apache.org> wrote:
> > >
> > > On 6/23/06, Nima <de...@gmail.com> 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(20000);
> > > >         factory.setRepository(new File(""));
> > > >         ServletFileUpload upload = new ServletFileUpload(factory);
> > > >         upload.setSizeMax(20000);
> > > >         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
> > > >
> > > > <form action="FrontController" method="POST"
> > > > enctype="multipart/form-data">
> > > >         <input type="file" value="filename" />
> > > >         <input type="submit" value="Submit" />
> > > > </form>
> > > >
> > > >
> > >
> > >
> >
> >
>
>

Re: Fileupload question

Posted by Martin Cooper <ma...@apache.org>.
On 6/23/06, Nima <de...@gmail.com> 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 <ma...@apache.org> wrote:
> >
> > On 6/23/06, Nima <de...@gmail.com> 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(20000);
> > >         factory.setRepository(new File(""));
> > >         ServletFileUpload upload = new ServletFileUpload(factory);
> > >         upload.setSizeMax(20000);
> > >         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
> > >
> > > <form action="FrontController" method="POST"
> > > enctype="multipart/form-data">
> > >         <input type="file" value="filename" />
> > >         <input type="submit" value="Submit" />
> > > </form>
> > >
> > >
> >
> >
>
>

Re: Fileupload question

Posted by Nima <de...@gmail.com>.
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 <ma...@apache.org> wrote:
>
> On 6/23/06, Nima <de...@gmail.com> 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(20000);
> >         factory.setRepository(new File(""));
> >         ServletFileUpload upload = new ServletFileUpload(factory);
> >         upload.setSizeMax(20000);
> >         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
> >
> > <form action="FrontController" method="POST"
> > enctype="multipart/form-data">
> >         <input type="file" value="filename" />
> >         <input type="submit" value="Submit" />
> > </form>
> >
> >
>
>

Re: Fileupload question

Posted by Martin Cooper <ma...@apache.org>.
On 6/23/06, Nima <de...@gmail.com> 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(20000);
>         factory.setRepository(new File(""));
>         ServletFileUpload upload = new ServletFileUpload(factory);
>         upload.setSizeMax(20000);
>         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
>
> <form action="FrontController" method="POST"
> enctype="multipart/form-data">
>         <input type="file" value="filename" />
>         <input type="submit" value="Submit" />
> </form>
>
>