JSP Tutorial - JSP File Upload








We can upload text file, binary or image file to the server by using JSP.

The following sections how to create client side form and server script to upload a file to the server.

Example

The following html code below creates a form to upload file.

The form method attribute should be set to POST method and we cannot use GET method to upload a file.

The form enctype attribute is set to multipart/form-data.

The form action attribute is set to a JSP file which would handle file uploading from server.

<html>
<body>
Select a file to upload: <br />
<form action="fileupload.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

The next step is to create Server side logic in JSP page.

The JSP file use the file-upload and common-io libraries.

Include the commons-fileupload.jar and commons-fileupload.jar in the classpath.

We can download those files from http://commons.apache.org/.

Create directories c:\temp and c:\apache-tomcat\webapps\data in advance.

Here is the JSP file to handle the file upload.

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>

<%
   File file ;
   int maxFileSize = 5000 * 1024;
   int maxMemSize = 5000 * 1024;
   String filePath = "c:/apache-tomcat/webapps/data/";

   String contentType = request.getContentType();
   if ((contentType.indexOf("multipart/form-data") >= 0)) {

      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(maxMemSize);
      factory.setRepository(new File("c:\\temp"));
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setSizeMax( maxFileSize );
      try{ 
         List fileItems = upload.parseRequest(request);
         Iterator i = fileItems.iterator();
         out.println("<html>");
         out.println("<body>");
         while ( i.hasNext () ) 
         {
            FileItem fi = (FileItem)i.next();
            if ( !fi.isFormField () )  {
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                file = new File( filePath + "yourFileName") ;
                fi.write( file ) ;
                out.println("Uploaded Filename: " + filePath + fileName + "<br>");
            }
         }
         out.println("</body>");
         out.println("</html>");
      }catch(Exception ex) {
         System.out.println(ex);
      }
   }else{
      out.println("<html>");
      out.println("<body>");
      out.println("<p>No file uploaded</p>"); 
      out.println("</body>");
      out.println("</html>");
   }
%>

Try http://localhost:8080/UploadFile.htm and upload a small file. The file should be uploaded in c:/apache-tomcat/webapps/data/ directory.