FileUpload.java :  » Web-Framework » rife-1.6.1 » com » uwyn » rife » engine » testelements » submission » Java Open Source

Java Open Source » Web Framework » rife 1.6.1 
rife 1.6.1 » com » uwyn » rife » engine » testelements » submission » FileUpload.java
/*
 * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id: FileUpload.java 3634 2007-01-08 21:42:24Z gbevin $
 */
package com.uwyn.rife.engine.testelements.submission;

import com.uwyn.rife.engine.Element;
import com.uwyn.rife.engine.UploadedFile;
import com.uwyn.rife.engine.exceptions.EngineException;
import com.uwyn.rife.tools.FileUtils;
import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;

public class FileUpload extends Element
{
  public void processElement()
  {
    if (hasSubmission("upload"))
    {
      if (hasUploadedFile("doc1"))
      {
        if (isFileEmpty("doc1"))
        {
          if (getUploadedFile("doc1").wasSizeExceeded())
          {
            print("file 1 size exceeded");
          }
          else
          {
            print("empty file 1");
          }
        }
        else
        {
          UploadedFile[] files = getUploadedFiles("doc1");
          for (int i = 0; i < files.length; i++)
          {
            try
            {
              print(FileUtils.readString(files[i].getFile()));
            }
            catch (FileUtilsErrorException e)
            {
              throw new EngineException(e);
            }
            
            if (i < files.length-1)
            {
              print(",");
            }
          }
        }
      }
      else
      {
        print("no file 1");
      }
      print(";");
      if (hasUploadedFile("doc2"))
      {
        if (isFileEmpty("doc2"))
        {
          if (getUploadedFile("doc2").wasSizeExceeded())
          {
            print("file 2 size exceeded");
          }
          else
          {
            print("empty file 2");
          }
        }
        else
        {
          UploadedFile[] files = getUploadedFiles("doc2");
          for (int i = 0; i < files.length; i++)
          {
            try
            {
              print(FileUtils.readString(files[i].getFile()));
            }
            catch (FileUtilsErrorException e)
            {
              throw new EngineException(e);
            }
            
            if (i < files.length-1)
            {
              print(",");
            }
          }
        }
      }
      else
      {
        print("no file 2");
      }
      print(";");
      print(getParameter("purpose"));
      return;
    }
    
    print("<html><body>\n");
    print("<form action=\""+getSubmissionFormUrl()+"\" method=\"post\" enctype=\"multipart/form-data\">\n");
    print(getSubmissionFormParameters("upload")+"\n");
    print("<input name=\"purpose\" type=\"text\">\n");
    print("<input name=\"doc1\" type=\"file\">\n");
    print("<input name=\"doc1\" type=\"file\">\n");
    print("<input name=\"doc2\" type=\"file\">\n");
    print("<input type=\"submit\">\n");
    print("</form>\n");
    print("</body></html>\n");
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.