Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple file upload in Jsp using Apache commons file upload API

I am not able to upload multiple files.I am using this code. Here itr.hasNext() is returning false.

FileUpload.jsp

<%@ page import="java.util.*" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.File" %>
   <%@ page import="org.apache.commons.fileupload.servlet.*" %>
   <%@ page import="org.apache.commons.fileupload.disk.*"%>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
   <center>
<table border="2">
        <tr>
        <td>
        <h1>Your files  uploaded </h1>
        </td>
        </tr>
   <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    System.out.println("isMultipart="+isMultipart);
    System.out.println(config.getServletContext());

           FileItemFactory factory = new DiskFileItemFactory();
           ServletFileUpload upload = new ServletFileUpload(factory);
           System.out.println(upload.parseRequest(request));    
           List items=null;
           try {
                items = upload.parseRequest(request);
           } catch (FileUploadException e) {
                   e.printStackTrace();
           }
           Iterator itr = items.iterator();
           while (itr.hasNext()) {
           FileItem item = (FileItem)(itr.next());
           out.println("itr");
           if (item.isFormField()) {
                    try{
                        String field=item.getFieldName();
                        String value=item.getString();
                        System.out.println("field="+value);
                    }
                    catch(Exception e){}
           } 
           else {
                   try {
                       out.println("nor done");
                           String itemName = item.getName();
                           out.println("1done");
                           File savedFile = new File("/home/saurabh/assignments/"+itemName);
                           item.write(savedFile);  
                           out.println("done");
                   } catch (Exception e) {
                           e.printStackTrace();
                   }
           }
           }

   %>
    </table>
   </center>

html file is:-

<html>
 <head><title>Upload page</title></head></p> <p><body>
 <form action="FileUpload.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="2">
       <tr>
               <td align="center"><b>Multipale file Uploade</td>
           </tr>
       <tr>
               <td>
                       Specify file: <input name="file" type="file" id="file">
                   <td>
           </tr>
           <tr>
              <td>
                     Specify file:<input name="file" type="file" id="file">
                  </td>
        <tr>
                   <td>
                      Specify file:<input name="file" type="file" id="file">
                   </td>
                 </tr>
                 <tr>
                    <td align="center">
               <input type="submit" name="Submit" value="Submit files"/>
                        </td>
                 </tr>
    </table>
        <center>
 </form>
 </body>
 </html>

How is this caused and how can I solve this?

like image 773
saurabh Avatar asked Sep 08 '26 23:09

saurabh


1 Answers

You're parsing the request twice. The first time to print the items and the second time to really get the items for processing. This isn't going to work. It can be parsed only once.

Remove the following useless line

System.out.println(upload.parseRequest(request));  

If you really intend to print the parsed items for some reason, then you should do

System.out.println(items);

after you have parsed the request once inside that try block.


Unrelated to the concrete problem, a JSP is not the best place for this job. Do this job in a servlet and collect the results in some map/bean which you put in the request scope and then forward the request to a JSP to display the results.

like image 86
BalusC Avatar answered Sep 11 '26 13:09

BalusC