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(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>


Reply via email to