Code to upload multiple files simultaneously using JSP, Servlet .

Step 1 : Create a jsp page.

index.jsp

<%-- 
    Document   : index
    Created on : 21 Sep, 2013, 5:58:32 PM
    Author     : snlkjha
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="UploadServlet" method="post" enctype="multipart/form-data">
            <input type="file" id="file" name="file1" accept="image/*"  multiple="muliple" required/><br>
            <input type="submit"/>
           <br><br> ${requestScope.message}
        </form>
    </body>
</html>

step 2: 

a. Download cos-multipart.jar file
b. Extract this file -> cos-multipart
c. Right Click Libraries folder of your project ->Add JAR/Folder
d. Browse cos-multipart jar file

step 3 : 

a. Create a Package in the source packages of project and give a name of your choice(Here Package name is MyPackage).
b. Right click the ur package->select servlet->Name it UploadServlet
c. Copy & Paste the same

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyPackage;

import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import com.oreilly.servlet.multipart.FilePart;

public class UploadServlet extends HttpServlet {

    private String fileSavePath;
    private static final String UPLOAD_DIRECTORY = "Upload";

    public void init() {
        fileSavePath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;/*save uploaded files to a 'Upload' directory in the web app*/
        if (!(new File(fileSavePath)).exists()) {
            (new File(fileSavePath)).mkdir();    // creates the directory if it does not exist        
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        String resp = "";
        int i = 1;
        resp += "<br>Here is information about uploaded files.<br>";
        try {
            MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024);  /* file limit size of 1GB*/
            Part _part;
            while ((_part = parser.readNextPart()) != null) {
                if (_part.isFile()) {
                    FilePart fPart = (FilePart) _part;  // get some info about the file
                    String name = fPart.getFileName();
                    if (name != null) {
                        long fileSize = fPart.writeTo(new File(fileSavePath));
                        resp += i++ + ". " + fPart.getFilePath() + "[" + fileSize / 1024 + " KB]<br>";
                    } else {
                        resp = "<br>The user did not upload a file for this part.";
                    }
                }
            }// end while 
        } catch (java.io.IOException ioe) {
            resp = ioe.getMessage();
        }
        request.setAttribute("message", resp);
        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

 step 4: run the project :

Click browse and select multiple images using ctrl and press enter/click open.


Click Submit


Step 5 : Now goto the project directory, open project folder->build->web->Upload to see the uploaded files.

** Instruction based on Netbeans with apache tomcat server.

*************************  Enjoy Post, Happy Coding  *****************************





Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

STRING PALINDROME USING STACK AND QUEUE