1

I am trying to upload an image using a servlet but I am not able to do that. There is no error in the server console.

Here is my Servlet code

    if (ServletFileUpload.isMultipartContent(request)) {
        try {
            List<FileItem> multiparts = new ServletFileUpload(
                    new DiskFileItemFactory()).parseRequest(request);
            System.out.println(items);    // this is printed as empty list
            for (FileItem item : multiparts) {
                System.out.print(DIR);
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(DIR + "\\" + name));
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
            request.getRequestDispatcher("/message/success/Failure.jsp").forward(request, response);
        }

    } else {
            request.getRequestDispatcher("/message/success/NotSupported.jsp").forward(request, response);
    }

My HTML

    <form action="ChangeAvtar" method="post">
        <a onclick="document.getElementById('file').click()" >Edit</a>
        <input type="file" id="file" />
        <input type="submit">
    </form>

The reason I can find out is that the items list is empty so its not going into the for loop but not sure why.

Thank you.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • See if this links helps you - http://stackoverflow.com/questions/13881272/servletfileuploadparserequestrequest-returns-an-empty-list – Abdul Salam Dec 10 '13 at 06:39

1 Answers1

0

Try to use ---> enctype="multipart/form-data" in the form tag in html..