Why can't upload three files with FileUpload ?

Posted by Valter Henrique on Stack Overflow See other posts from Stack Overflow or by Valter Henrique
Published on 2011-03-05T23:55:54Z Indexed on 2011/03/06 0:10 UTC
Read the original article Hit count: 362

Filed under:
|
|
|
|

Hi everyone, i'm trying to upload three images to my server, is working, but upload always the last file selected by the user, not the three selected.

Here's my code:

protected void doPost(HttpServletRequest request, HttpServletResponse response){
    boolean multipart = ServletFileUpload.isMultipartContent(request);

    if (multipart) {
        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();

        fileItemFactory.setSizeThreshold(5 * 1024 * 1024); //5 MB
        fileItemFactory.setRepository(tmpDir);

        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
        try {
            List items = uploadHandler.parseRequest(request);

            Iterator itr = items.iterator();

            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();

                File file = new File(dir, generateNewName());
                item.write(file);
            }
        } catch (FileUploadException ex) {
        } catch (Exception ex) {
        }
    }
}

-- UPDATE:

<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <form action="Upload" method="post" enctype="multipart/form-data">
            <input type="file" name="file1" />
            <br />

            <input type="file" name="file2" />
            <br />

            <input type="file" name="file3" />
            <br />

            <input type="submit" value="Enviar" />

        </form>
    </body>
</html>

Best regards, Valter Henrique.

© Stack Overflow or respective owner

Related posts about java

Related posts about jsp