<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%
    System.out.println("\n\n*****ProcessFileUpload.jsp request********");
    System.out.println("Content Type: "+request.getContentType());

    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    
    // If file size exceeds this value, a FileUploadException is thrown
    upload.setSizeMax(1000000);

	//Get a list of items from the request
	//Items can be either form fields or files
    List fileItems = upload.parseRequest(request);
    Iterator itr = fileItems.iterator();
    
    //Determine the current home directory, append the assets/gallery path
    //to figure out where to place the file
    String requestPath = request.getServletPath();
	System.out.println("\nREQUEST: " + requestPath);
	int lastSlash = requestPath.lastIndexOf("/");
	String gallery = application.getRealPath(requestPath.substring(0, lastSlash) +
		"/assets/gallery");
	System.out.println("GALLERY: " + gallery);
	
    /*The fileItems List contains FileItem instances, each of
    *which can be either a form field or a file.  Determine which
    *calling FileItem.isFormField(); if it returns false, then it's 
    *a file.  Requests generated by Flash Player's FileReference can 
    *only contain a single file, so we only write 1 file to disk each
    *time this file is called
    */
    while(itr.hasNext()) {

      FileItem fi = (FileItem)itr.next();

      if(!fi.isFormField()) {
        System.out.println("FIELD NAME: "+fi.getFieldName());
        System.out.println("NAME: "+fi.getName());
        System.out.println("SIZE: "+fi.getSize());

        File fNew= new File(gallery, fi.getName());
        System.out.println("PATH: "+fNew.getAbsolutePath());
        fi.write(fNew);
        break;
      }
    }
%>

