title: File upload to server using java
body: <p>I'm trying to upload a file to a server.I'm using apache tomcat and java .
  Following is my code,</p>
  
  <p>Fileuploadservlet.java:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  
  @WebServlet("/UploadServlet")
  
  
  public class FileUploadServlet extends HttpServlet {
  
  private boolean isMultipart;
  private String filePath;
  private int maxFileSize = 1000 * 1024;
  private int maxMemSize = 40 * 1024;
  private File file ;
  
  public void init( ){
    // Get the file location where it would be stored.
    filePath = getServletContext().getInitParameter("file-upload"); 
  }
  
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
     doPost(request,response);
  }
  
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
  
    // Check that we have a file upload request
    isMultipart = ServletFileUpload.isMultipartContent(request);
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter( );
  
    if( !isMultipart ) {
       out.println("&lt;html&gt;");
       out.println("&lt;head&gt;");
       out.println("&lt;title&gt;Servlet upload&lt;/title&gt;");  
       out.println("&lt;/head&gt;");
       out.println("&lt;body&gt;");
       out.println("&lt;p&gt;No file uploaded&lt;/p&gt;"); 
       out.println("&lt;/body&gt;");
       out.println("&lt;/html&gt;");
       return;
    }
  
    DiskFileItemFactory factory = new DiskFileItemFactory();
  
    // maximum size that will be stored in memory
    factory.setSizeThreshold(maxMemSize);
  
    // Location to save data that is larger than maxMemSize.
    factory.setRepository(new File("C:\\temp"));
  
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
  
    // maximum file size to be uploaded.
    upload.setSizeMax( maxFileSize );
  
    try { 
       // Parse the request to get file items.
       List fileItems = upload.parseRequest(request);
  
       // Process the uploaded file items
       Iterator i = fileItems.iterator();
  
       out.println("&lt;html&gt;");
       out.println("&lt;head&gt;");
       out.println("&lt;title&gt;Servlet upload&lt;/title&gt;");  
       out.println("&lt;/head&gt;");
       out.println("&lt;body&gt;");
  
       while ( i.hasNext () ) {
          FileItem fi = (FileItem)i.next();
          if ( !fi.isFormField () ) {
             // Get the uploaded file parameters
             String fieldName = fi.getFieldName();
             String fileName = fi.getName();
             String contentType = fi.getContentType();
             boolean isInMemory = fi.isInMemory();
             long sizeInBytes = fi.getSize();
  
             // Write the file
             if( fileName.lastIndexOf("\\") &gt;= 0 ) {
                file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
             } else {
                file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
             }
             fi.write( file ) ;
             out.println("Uploaded Filename: " + fileName + "&lt;br&gt;");
          }
       }
       out.println("&lt;/body&gt;");
       out.println("&lt;/html&gt;");
       } catch(Exception ex) {
          System.out.println(ex);
       }
    }
  </code></pre>
  
  <p>}</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  &lt;display-name&gt;FileUploadServletExample&lt;/display-name&gt;
  
  &lt;servlet&gt;
  &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
  &lt;servlet-class&gt;UploadServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/UploadServlet&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;    
  
  &lt;context-param&gt; 
    &lt;description&gt;Location to store uploaded file&lt;/description&gt; 
    &lt;param-name&gt;file-upload&lt;/param-name&gt; 
    &lt;param-value&gt;
       C:\Users\e48265\apache-tomcat-7.0.81\webapps\data\
    &lt;/param-value&gt; 
  &lt;/context-param&gt;
  
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;upload.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>upload.jsp:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;File Upload Demo&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;center&gt;
  &lt;h3&gt;File Upload:&lt;/h3&gt;
    Select a file to upload: &lt;br /&gt;
    &lt;form action = "UploadServlet" method = "post" enctype = "multipart/form-
   data"&gt;
       &lt;input type = "file" name = "file" size = "50" /&gt;
       &lt;br /&gt;
       &lt;input type = "submit" value = "Upload File" /&gt;
    &lt;/form&gt;
   &lt;/center&gt;
    &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>I'm getting the following output after running upload.jsp file and clicking upload button,
  java.io.FileNotFoundException: null\quizlet.txt (The system cannot find the path specified).</p>
  
  <p>Can anyone help me with ?</p>
  
  <p>Note:quizlet.txt is my file that I want to upload. </p>