5

I want to offer to upload multiple files clicking upload button only once. Can I use multipart to send files? If yes, then how?

P.S.: I don't want to use flash or send one file at a time.

3 Answers 3

9

In you HTML, you can do the following :

<input accept="image/jpeg,image/gif,image/png" type="file" name="upload[]" multiple/>

adding multiple to the end of your input grants you what you want.

Sign up to request clarification or add additional context in comments.

Comments

7

Hope This Helps...

//JSP File

<html>
<head><title>Upload page</title></head></p> <p><body>
<form action="upload_file" method="post" enctype="multipart/form-data" name="form1" id="form1">
<center>
 Specify file: <input name="file" type="file" id="file">
 Specify file: <input name="file" type="file" id="file">
 Specify file:<input name="file" type="file" id="file">
 <input type="submit" name="Submit" value="Submit files"/>
<center>
</form>
</body>
</html>


//Servlet Page

import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
   if (!isMultipart) {
   }
   else{
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      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();
           if (item.isFormField()) {
           } else {
           try {
              String itemName = item.getName();
              File savedFile = new File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+itemName);
              item.write(savedFile);
              out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");
              } catch (Exception e) {
                   e.printStackTrace();
              }
       }
}

3 Comments

Thanks for your response but I cannot use spring I can only use servlets 2.5
Updated answer kindly verify it
No, I cannot use apache commons(or any external library), Also I have to monitor progress of ho much file has been uploaded. that I have to handle in JS :)
3

Yes, you can. Try apache fileupload library, you may refer to this question on stackoverflow: Multiple file upload in Jsp using Apache commons file upload API or this full example. Also you may take use of Spring by referencing this article.

1 Comment

Thanks for your response but I don't want to use external libraries or API's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.