0

I am trying to upload file using java web application using DiskFileItemFactory.But the problem is it was not getting any file.It shows empty array in iteration.

iter.size() is 0. See below of my code.

public ModelAndView upload( ModelMap model, HttpSession session, HttpServletRequest request, HttpServletResponse resp) throws IOException, ServletException, FileUploadException {
if (ServletFileUpload.isMultipartContent(request)) {

    System.out.println("file available");
}

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
    //  ResourceImpl resource = new ResourceImpl();
    try {
        //Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = upload.parseRequest(request);
        System.out.println(items.size());
        Iterator iter = items.iterator();
        while (iter.hasNext()) {

            FileItem item = (FileItem) iter.next();
        }

    } catch (Exception e) {

    }
}

My form is;

 <form method="post" action="/DropBox/upload.html" enctype="multipart/form-data">
     Select file to upload: <input type="file" name="uploadFile" />
     <br/><br/>
     <input type="submit" value="Upload" />
 </form>

Where I am doing wrong. plaese help me. thanq.

Tiny
  • 27,221
  • 105
  • 339
  • 599
kamesh
  • 27
  • 5
  • Now check it problem to here List items = upload.parseRequest(request).Shows 0 siz. – kamesh Dec 02 '14 at 10:29
  • Does it make a difference, if you declare the list using using a generic type such as `List items = upload.parseRequest(request);`? BTW this is a detailed [answer](http://stackoverflow.com/a/2424824/1391249) about uploading files using the Servlet API. – Tiny Dec 02 '14 at 11:06
  • Hi.. Tiny.I have tried with generic type.But it is also showing same result. – kamesh Dec 02 '14 at 11:19
  • 1
    If it is Spring MVC (given the impression by the return type `ModelAndView` of one of the methods in your code) then, there a [facility](http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/mvc.html#mvc-multipart) out of the box to upload multipart contents. There is no need to repeat the same code. – Tiny Dec 02 '14 at 11:27
  • So what i have to do....? – kamesh Dec 02 '14 at 11:33
  • Yup! learning lessons like me :) You need to configure [`org.springframework.web.multipart.commons.CommonsMultipartResolver`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/commons/CommonsMultipartResolver.html) in your Spring config file (may be named `disparcher-servlet.xml`) Google could give you examples [such as](http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/). – Tiny Dec 02 '14 at 11:56
  • Ya I have configured that......that is not a problem to here. – kamesh Dec 02 '14 at 12:22
  • That should be working then :) Does it? – Tiny Dec 02 '14 at 13:41

1 Answers1

0

You should try for each. it does well.

if (ServletFileUpload.isMultipartContent(request)) {

    try {
        List<FileItem> multipart = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

        for (FileItem item : multipart) {

            if (!item.isFormField()) {

                filename = new File(item.getName()).getName();
                folder = "path";
                File file = new File(folder);

                if (!file.exists()) {
                    file.mkdir();

                }
            }

            item.write(new File(folder + "/" + filename));
        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
RDY
  • 613
  • 1
  • 9
  • 25