The following is an example of a component that handles file uploads. One way to use this component is through a form element such as the one presented in the previous example. The example here includes two alternatives, one that returns the uploaded file as a byte array and one (which is commented out) where the uploaded file is read from the input stream.

import atg.servlet.UploadedFile;
import java.io.*;
import atg.droplet.GenericFormHandler;
public class FileUploadComponent extends GenericFormHandler
{
  /**
   * This method is called when the form above is submitted.  This code makes
   * sure that it has an appropriate object and then pass it along for further
   * processing.
   * @param Object either an UploadedFile or an UploadedFile[]
   **/
  public void setUploadProperty(Object fileObject) {
    if(fileObject == null) {
        System.err.println("**** ERROR: FileUploadDroplet received a NULL file.");
        return;
      }

    if (fileObject instanceof UploadedFile[]) {
      System.out.println("Reading in UploadedFile[]");
      readUpFiles((UploadedFile[]) fileObject);
    }else if (fileObject instanceof UploadedFile){
      readUpFiles(new UploadedFile[]{(UploadedFile)fileObject});
    }else{
      System.err.print
        ("**** ERROR: FileUploadDroplet received an Object which is "
         + "neither an UploadedFile or an UploadedFile[].");
    }
  }
/**
   * Returns property UploadProperty
   **/
  public Object getUploadProperty() {
    // return null since we don't need to maintain a
    // reference to the original uploaded file(s)
    return null;
  }
//-------------------------------------
  /**
   * Here you can access the data in the uploaded file(s). You
   * should get the data from the uploaded file before the
   * request is complete.  If the file is large, it is stored as a temporary
   * file on disk, and this file is removed when the request is complete.
   * @param UploadedFile[] the uploaded file(s)
   **/
  void readUpFiles(UploadedFile[] pFiles){
    UploadedFile upFile = null;
    String clientFilePath = null;
    String fileName = null;
    File localFile = null;
    FileOutputStream fos = null;
    byte[] fileData = null;

    for (int i = 0; i < pFiles.length; i++){
      upFile = pFiles[i];
      clientFilePath = upFile.getFilename();

      // Check that file uploaded is not size 0.
      if(upFile.getFileSize() <= 0){
        System.err.println
          (" FileUploadDroplet Cannot upload - file has length 0: "
          + clientFilePath);
        return;
      }
      /**
       * Extract the FilePath, which is the file location provided by the
       * browser client. Convert the file separator character to use the one
       * accepted by the web client's Operating system.
       **/

      String otherSeparator = "/";
      if ( "/".equals(File.separator))
        otherSeparator = "\\";
        String convertedClientFilePath = atg.core.util.StringUtils.replace
        (clientFilePath,otherSeparator,File.separator);

      fileName =
        new String
        (convertedClientFilePath.substring
        (convertedClientFilePath.lastIndexOf
        (File.separator)+1));

      // Construct a local file (using the uploaded file directory)
      localFile = new File
        (mUploadDirectory
        + File.separator
        + fileName);

      // You can either get the file as an array of bytes ...
      try {
        fileData = upFile.toByteArray();
        System.out.println
          (" ** client filename: " + clientFilePath);
        System.out.println
          (" ** client file is " + upFile.getFileSize() + " bytes long.");
        fos = new FileOutputStream(localFile);
        fos.write(fileData);
        fos.flush();
      }
      catch (IOException e) {
        System.err.println("FileUploadDroplet failed");
        e.printStackTrace();
      }
      finally {
        if (fos != null){
          try {
            fos.close();
          }catch(IOException exc) {
            exc.printStackTrace();
          }
        }//end try/catch
      }//end finally

      // ... or you can read the data yourself from the input stream.
      /**
      try{
        InputStream is = upFile.getInputStream();
        ...
      }
      catch (IOException e) {
      }  **/
    }// end for
  }// end readUpFiles

  //-------------------------------------
  // property: UploadDirectory
  // where we will put the uploaded file
  String mUploadDirectory;

  /**
   * Sets property UploadDirectory
   **/
  public void setUploadDirectory(String pUploadDirectory) {
    mUploadDirectory = pUploadDirectory;
  }

  /**
   * Returns property UploadDirectory
   **/
  public String getUploadDirectory() {
    return mUploadDirectory;
  }

}