JSP example

Top  Previous  Next

The example below demonstrates how using a JSP script you can process files submitted by HTTP File Upload Applet.  The source code for this example may be found in the examples/jsp/receiver.jsp 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

 

<%@ page import="java.io.*" %>

<%@ page import="java.util.*" %>

<%@ page import="org.apache.commons.fileupload.*" %>

<%

       String currentDirectory = "";

       String file = "";

 

       // Define upload directory, notice that permissions to write are needed for upload directory

       String uploadDir = "/var/uploads/";

       // Create a new file upload handler

       DiskFileUpload upload = new DiskFileUpload();

 

       try {

           List items = upload.parseRequest(request);

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

               FileItem item = (FileItem) 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();

                       

                   currentDirectory = "";

                   file = "";

                   //gets a directory array

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

                   if (data.length != 1) {

                          //Starts File name index

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

                       // The file name

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

                       // The directory name

                       currentDirectory = fileName.substring(0, index);

                   } else {

                          // There is not a directory name

                       file = fileName;

                   }

 

                   String newDirectory = uploadDir + currentDirectory;

                   File directoryFile = new File(newDirectory);

                   //Creates the directory when it is necessary

                   if (!directoryFile.isDirectory()) {

                       directoryFile.mkdirs();

                   }

 

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

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

                           + file

                           : uploadDir + currentDirectory + "\\" + 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();

       } catch (Exception e) {

           e.printStackTrace();

       }

%>

 

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.Upload receiver.jsp file to appropriate directory on application server.
4.Set url parameter in HTML applet code to point to url of this script e.g. http://www.jscape.com/appname/receiver.jsp
5.Done.  You may now run HTTP File Upload Applet

  

See also

 

Specifying the upload URL