Hi Octavia,
you are right, "wrapper" doesn't really meet the thing, because i wouldn't let the FormFileWrapper delegate to the methods of a FormFile-Member but copy the byte [] etc. FormFile is an Interface and no class, otherwise you could extend FormFile and make the the InputStream transient. In fact, you dont know which implementation of FormFile you will get.
I have got no code example yet, but i am concerned with the same thing right now, so:



import org.apache.log4j.Logger; import org.apache.struts.upload.FormFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;

/**
* Created by IntelliJ IDEA.
* User: astahlhut
* Date: 11.05.2004
* Time: 00:32:04
*/
public class FormFileValueHolder implements Serializable {
   private String contentType;
   private String fileName;
   private byte[] fileData;
   private int fileSize;

private static Logger trace = Logger.getLogger(FormFileValueHolder.class);


public FormFileValueHolder(FormFile ff) throws IOException{ setContentType(ff.getContentType()); setFileSize(ff.getFileSize()); setFileName(ff.getFileName());

try {
this.fileData = ff.getFileData();
} catch (IOException e) {
e.printStackTrace();
trace.error("An Exception occured while building FormFileValueHolder. Creation skipped: " + e);
throw e;
}
}


   /**
    * Returns the content type for this file.
    *
    * @return A String representing content type.
    */
   public String getContentType() {
       return contentType;
   }

   /**
    * Sets the content type for this file.
    *
    * @param contentType The content type for the file.
    */
   public void setContentType(String contentType) {
       this.contentType = contentType;
   }

   /**
    * Returns the size of this file.
    *
    * @return The size of the file, in bytes.
    */
   public int getFileSize() {
       return fileSize;
   }

   /**
    * Sets the file size.
    *
    * @param fileSize The size of the file, in bytes,
    */
   public void setFileSize(int fileSize) {
       this.fileSize = fileSize;
   }

/**
* Returns the file name of this file. This is the base name of the file,
* as supplied by the user when the file was uploaded.
*
* @return The base file name.
*/
public String getFileName() {
return fileName;
}


   /**
    * Sets the file name of this file.
    *
    * @param fileName The base file name.
    */
   public void setFileName(String fileName) {
       this.fileName = fileName;
   }

/**
* Returns the data for the entire file as byte array. Care is needed when
* using this method, since a large upload could easily exhaust available
* memory. The preferred method for accessing the file data is
*
* @return The file data as a byte array.
* @throws java.io.FileNotFoundException if the uploaded file is not found.
* @throws java.io.IOException if an error occurred while reading the
* file.
*/
public byte[] getFileData() throws FileNotFoundException, IOException {
return fileData;
}


   /**
    * Destroys all content for the uploaded file, including any underlying
    * data files.
    */
   public void destroy() {
       setContentType(null);
       setFileName(null);
       fileData = null;
   }
}

But take care of the case described in the JavaDoc of getFileData()! I am not sure why you ant to serilize this, but I would only do it if you got not much traffic, the mxFileUploadSize is very small amd you got lots of memory. And, if you use it, call destroy as early as possible. Each FormFileValueHolder will eat roundabout as much memory as the file it encapulates.

Regards Axel

PS: I just rented a server to build a forum, maybe mailing list later on, for tips and code snippets like that. What is really important to me is to have a place where you get rid of that OT-problem, where you hav at least a special section where everybody can post everythinground about this technology stack (Struts, Hibernate, Castor, JSTL, JSF, MySQL, Tomcat and so on). You will reach this place in a day or three at: www.javacookbook.de

Octavia Yung wrote:

Thanks, Axel.  So would the FormFileWrapper just be another class which
houses the Struts FormFile class?  What fields should I be copying...I'm
confused by your point.  Is there any sample code available?

Besides the method mentioned above, are there other alternatives to approach
the problem?  Thanks again!

Octavia


----- Original Message ----- From: "Axel Stahlhut" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Monday, May 10, 2004 2:04 PM
Subject: Re: serialization of a FormFile object





Hi.

You could implement a FormFileWrapper, geting a FormFile in Constructor.
Then you may copy all fields except InputStream (which makes in fact no
sense, because it is definetly lost after e.g. restart of App) and let
the wrapper implement Serializable. The wrapper can now be stored into
your Session or Session-scoped FormBean.

Regards Axel

Octavia Yung wrote:



Hi All,

I was wondering if is it be possible to serialize a FormFile object


within an ActionForm (i.e. the FormFile object is being put on the Session)?
If so, how would one go about doing this?


Thanks much for your help!

Octavia




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





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





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



Reply via email to