Date: 2005-03-03T00:03:17
   Editor: DakotaJack
   Wiki: Apache Struts Wiki
   Page: StrutsUpload
   URL: http://wiki.apache.org/struts/StrutsUpload

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -8,20 +8,22 @@
 
 === Framework Code ===
 
- 1. !MultipartFile Interface
- 2. !MultipartData Interface
- 3. !MultipartHandler Interface
- 4. !MultipartUtil Class
+ 1. !MultipartFile
+ 2. !MultipartData
+ 3. !MultipartHandler
+ 4. !MultipartUtil
 
 === Sample Implementation Code ===
 
- 1. !UploadMultipartFile Class
- 2. !UploadMultipartData Class
- 3. !UploadMultipartHandler Class
+ 1. !UploadMultipartFile (implements !MultipartFile)
+ 2. !UploadMultipartData (implements !MultipartData)
+ 3. !UploadMultipartHandler (implements !MultipartHandler)
 
 === Sample Application Code Pieces ===
 
- 1.  !UploadOutputStream 
+ 1.  !UploadOutputStream (extends 
org.apache.commons.fileupload.!DeferredFileOutputStream)
+ 2.  !UploadFileItem (implements org.apache.commons.fileupload.!FileItem)
+ 3.  !UploadFileItemFactory (extends 
org.apache.commons.fileupload.!DefaultFileItemFactory)
 
 == Code ==
 
@@ -385,6 +387,256 @@
         monitor.read(j);
       }
     }
+  }
+}
+}}}
+
+=== UploadFileItem ===
+
+{{{
+public class UploadFileItem
+    implements FileItem {
+  private static volatile int number = 0;
+  private UploadOutputStream  uos;
+  private File                tempDir;
+  private List                monitors;
+  private String              fieldName;
+  private String              contentType;
+  private String              fileName;
+  private byte []             data;
+  private int                 threshold;
+  private int                 totalSize;
+  private boolean             isFormField;
+
+  public UploadFileItem(String  fieldName,
+                        String  contentType,
+                        boolean isFormField,
+                        String  fileName,
+                        List    monitors,
+                        File    tempDir,
+                        int     threshold,
+                        int     totalSize) {
+    this.fieldName   = fieldName;
+    this.contentType = contentType;
+    this.isFormField = isFormField;
+    this.fileName    = fileName;
+    this.monitors    = monitors;
+    this.tempDir     = tempDir;
+    this.threshold   = threshold;
+    this.totalSize   = totalSize;
+  }
+
+  public InputStream getInputStream()
+      throws IOException {
+    if(!this.uos.isInMemory()) {
+      return new FileInputStream(uos.getFile());
+    }
+
+    if(data == null) {
+      data = this.uos.getData();
+    }
+
+    return new ByteArrayInputStream(data);
+  }
+
+  public String getContentType() {
+    return contentType;
+  }
+
+  public String getName() {
+    return fileName;
+  }
+
+  public boolean isInMemory() {
+    return uos.isInMemory();
+  }
+
+  public long getSize() {
+    if(data != null) {
+      return (long)data.length;
+    }
+
+    if(this.uos.isInMemory()) {
+      return (long)uos.getData().length;
+    } else {
+      return uos.getFile().length();
+    }
+  }
+
+  public byte[] get() {
+    if(uos.isInMemory()) {
+      if(data == null) {
+        data = uos.getData();
+      }
+
+      return data;
+    }
+
+    byte fisData[] = new byte[(int)getSize()];
+    FileInputStream fis = null;
+    try {
+      fis = new FileInputStream(uos.getFile());
+      fis.read(fisData);
+    } catch(IOException ioe) {
+      fisData = null;
+    } finally {
+      if(fis != null) {
+        try {
+          fis.close();
+        } catch(IOException ioe1) { }
+      }
+    }
+    return fisData;
+  }
+
+  public String getString(String encoding)
+      throws UnsupportedEncodingException {
+    return new String(get(), encoding);
+  }
+
+  public String getString() {
+    return new String(get());
+  }
+
+  public void write(File file)
+      throws Exception {
+    if(isInMemory()) {
+      FileOutputStream fos = null;
+      try {
+        fos = new FileOutputStream(file);
+        fos.write(get());
+      } finally {
+        if(fos != null) {
+          fos.close();
+        }
+      }
+    } else {
+      File outputFile = uos.getFile();
+
+      if(outputFile != null) {
+        if(!outputFile.renameTo(file)) {
+          BufferedInputStream  bis = null;
+          BufferedOutputStream bos = null;
+          try {
+            bis = new BufferedInputStream(new FileInputStream(outputFile));
+            bos = new BufferedOutputStream(new FileOutputStream(file));
+            byte streamData[] = new byte[UploadConstant.STREAM_BUFFER_SIZE];
+            for(int i = 0; (i = bis.read(streamData)) != -1;)
+              bos.write(streamData, 0, i);
+          } finally {
+            try {
+              bis.close();
+            } catch(IOException ioe) { }
+            try {
+              bos.close();
+            } catch(IOException ioe1) { }
+          }
+        }
+      } else {
+        throw new IOException(UploadConstant.WRITE_ERROR);
+      }
+    }
+  }
+
+  public void delete() {
+    data = null;
+    File file = uos.getFile();
+    if(file != null && file.exists()) {
+      file.delete();
+    }
+  }
+
+  public String  getFieldName() {
+    return fieldName;
+  }
+
+  public void setFieldName(String fieldName) {
+    this.fieldName = fieldName;
+  }
+
+  public boolean isFormField() {
+    return isFormField;
+  }
+
+  public void setFormField(boolean isFormField) {
+    this.isFormField = isFormField;
+  }
+
+  protected void finalize() {
+    File file = uos.getFile();
+    if(file != null && file.exists()) {
+      file.delete();
+    }
+  }
+
+  public OutputStream getOutputStream()
+      throws IOException {
+    if(uos == null) {
+      File file = tempDir;
+
+      if(file == null) {
+        file = new File(UploadConstant.PARSER_TEMP_DIR);
+      }
+
+      String fileName = UploadConstant.FILE_PREFIX + (number++) + 
UploadConstant.TMP_EXT;
+      File   outputFile = new File(file, fileName);
+
+      outputFile.deleteOnExit();
+
+      uos = new UploadOutputStream(threshold, outputFile, monitors, 
isFormField());
+
+      if(this.monitors != null && !isFormField()) {
+        for(int i = 0; i < monitors.size(); i++) {
+          Monitor monitor = (Monitor)monitors.get(i);
+          monitor.init(outputFile, totalSize, getContentType());
+        }
+
+      }
+    }
+    return this.uos;
+  }
+}
+}}}
+
+=== UploadFileItemFactory ===
+
+{{{
+public class UploadFileItemFactory
+    extends DefaultFileItemFactory {
+  private List monitors;
+  private int  contentLength;
+
+  public UploadFileItemFactory() {
+  }
+
+  public FileItem createItem(String  fieldName,
+                             String  contentType,
+                             boolean isFormField,
+                             String  fileName) {
+    return new UploadFileItem(fieldName,
+                              contentType,
+                              isFormField,
+                              fileName,
+                              monitors,
+                              getRepository(),
+                              getSizeThreshold(),
+                              contentLength);
+  }
+
+  public void setContentLength(int contentLength) {
+    this.contentLength = contentLength;
+  }
+
+  public int  getContentLength() {
+    return contentLength;
+  }
+
+  public void setMonitors(List monitors) {
+    this.monitors = monitors;
+  }
+
+  public List getMonitors() {
+    return this.monitors;
   }
 }
 }}}

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

Reply via email to