jvanzyl     01/05/13 17:29:48

  Modified:    src/java/org/apache/turbine/util/upload FileItem.java
  Log:
  - adding convenience method to write() the contents of the uploaded
    file to disk. instead of having to mess around now you just do
    the following:
  
    FileItem fileItem = data.getParameters().getFileItem("file");
    fileItem.write("/tmp/junk");
  
    returns a boolean so you can check to make sure the file
    was actually written to disk.
  
  Revision  Changes    Path
  1.9       +50 -1     
jakarta-turbine/src/java/org/apache/turbine/util/upload/FileItem.java
  
  Index: FileItem.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/util/upload/FileItem.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FileItem.java     2001/05/06 17:06:48     1.8
  +++ FileItem.java     2001/05/14 00:29:48     1.9
  @@ -59,6 +59,7 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  +import java.io.FileWriter;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.UnsupportedEncodingException;
  @@ -91,7 +92,8 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Rafal Krzewski</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sean Legassick</a>
  - * @version $Id: FileItem.java,v 1.8 2001/05/06 17:06:48 jvanzyl Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
  + * @version $Id: FileItem.java,v 1.9 2001/05/14 00:29:48 jvanzyl Exp $
    */
   public class FileItem implements DataSource
   {
  @@ -389,5 +391,52 @@
               item.byteStream = new ByteArrayOutputStream();
           }
           return item;
  +    }
  +
  +    /**
  +     * A convenience method to write an uploaded
  +     * file to disk. The client code is not concerned
  +     * whether or not the file is stored in memory,
  +     * in on disk in a temporary location. They just
  +     * want to write the uploaded file to disk.
  +     *
  +     * @param String full path to location where uploaded
  +     *               should be stored.
  +     */
  +    public boolean write(String file)
  +    {
  +        if (inMemory())
  +        {
  +            try
  +            {
  +                FileWriter writer = new FileWriter(file);
  +                writer.write(getString());
  +                return true;
  +            }
  +            catch (IOException ioe)
  +            {
  +                return false;
  +            }                
  +        }
  +        else if (storeLocation != null)
  +        {
  +            /*
  +             * The uploaded file is being stored on disk
  +             * in a temporary location so move it to the
  +             * desired file.
  +             */
  +            if (storeLocation.renameTo(new File(file)))
  +            {
  +                return true;
  +            }                
  +            else
  +            {
  +                return false;
  +            }                
  +        }
  +        else
  +        {
  +            return false;
  +        }            
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to