Servlet example

Top  Previous 

The example below demonstrates how using a Java servlet you can process files submitted by HTTP File Upload Applet.  The source code for this example may be found in the examples/servlet/Receiver.java file included with the HTTP File Upload Applet distribution.  The file upload classes found in the Jakarta FileUpload package are required for this example to work.  You may download the Jakarta FileUpload classes here:

 

http://jakarta.apache.org/commons/fileupload/

 

 

Example

 

 

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.*;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.commons.fileupload.*;

import org.apache.commons.fileupload.disk.*;

import org.apache.commons.fileupload.servlet.*;

 

 

 

public class Receiver extends HttpServlet {

 

  private String uploadDir = "/var/uploads/";

  private String file = "";

  private String currentDirectory = "";

 

 

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     

      // add connection closed header

       response.setHeader("Connection","close");      

      // Create PrintWriter object to write to

       PrintWriter out = response.getWriter();

      try {

          // Create a factory for disk-based file items

           FileItemFactory factory = new DiskFileItemFactory();

 

          // Create a new file upload handler

           ServletFileUpload upload = new ServletFileUpload(factory);

         

         

         

          // Parse the request, we got the list of items present in POST header

           List items = upload.parseRequest(request);

         

          for (int i = 0; i < items.size(); i++) {

               DiskFileItem item = (DiskFileItem) items.get(i);

              // As we are interested not in regular form fields, we filter only files

              if (!item.isFormField()) {

                  // the name of file to save

                   String fileName = item.getName();

                  //Split file name and directory name when this is specified

                         (fileName);

                  //creates the directory

                   createDirectory(this.currentDirectory);

                  // concat the default directory, the specified directory and the file name

                   String completeName = (this.currentDirectory.equals("")?

                           uploadDir + this.file:

                               uploadDir + this.currentDirectory +"\\"+ this.file);

                 

                  // save file

                   File uploadedFile = new File(completeName);

                   item.write(uploadedFile);

                  //if succeeded, print out the "success" code to notify applet the file was uploaded

                   out.print("RESP.100");

                  //flush the stream to speed up applet notification

                   out.flush();

               }

           }

       } catch (FileUploadException e) {

          //if failed for some reason, print out the "failed" code to notify applet the file wasn't uploaded

           out.print("RESP.200");

          // flush the stream to speed up applet notification

           out.flush();

           e.printStackTrace();

       } catch (Exception e) {

           e.printStackTrace();

       }

   }

 

  /**

    * Separates the file name and the directory name if the directory is specified

    *

    * @param fileName The file name which is sent from the client

    *

    */

  private void splitFileFromDirectory(String fileName){

       this.currentDirectory = "";

       this.file ="";

       String[] data = fileName.split("\\\\");

      if(data.length!=1){

          int index = fileName.lastIndexOf("\\");

           this.file = fileName.substring(index+1, fileName.length());

           this.currentDirectory = fileName.substring(0, index);

       }

      else{

           this.file = fileName;

       }

     

   }

 

  /**

    * Creates a new directory name if it does not exist

    *

    * @param directory The name of the directory

    */

  private void createDirectory(String directory){

       String newDirectory = uploadDir + directory;

       File directoryFile = new File(newDirectory);

      if(!directoryFile.isDirectory()){

           directoryFile.mkdirs();

       }

   }

}

 

 

Installation

 

1.Change uploadDir variable to directory path where files should be uploaded.  Make sure to include a / at the end and that the permissions to write are set properly.
2.Place required commons-fileupload-1.0.jar in WEB-INF/lib directory of application server.  This jar file is part of the Jakarta FileUpload package.
3.Compile Receiver.java code and upload compiled Receiver.class file to WEB-INF/classes directory on application server.
4.Configure servlet in web.xml descriptor /WEB-INF/web.xml

 

...

<servlet>

   <servlet-name>receiver</servlet-name>

   <servlet-class>Receiver</servlet-class>

 </servlet>

 

 <servlet-mapping>

   <url-pattern>/receiver</url-pattern>

   <servlet-name>receiver</servlet-name>

 </servlet-mapping>

...

 

5.Set url parameter in HTML applet code to point to url of this script e.g. http://www.jscape.com/appname/receiver
6.Done.  You may now run HTTP File Upload Applet

 

See also

 

Specifying the upload URL