commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McGrady <m...@michaelmcgrady.com>
Subject Re: DiskFileUpload.parseRequest returns no items
Date Tue, 05 Oct 2004 04:02:38 GMT
Hi, Martin,

You are not saying, are you, Martin that you cannot use Struts and 
"worry about how to use" commons classes yourself?  For example, as in:

package com.crackwillow.app.load.upload;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import com.crackwillow.app.load.upload.UploadFileItem;
import com.crackwillow.app.load.upload.UploadConstant;
import com.crackwillow.log.StdOut;

public class UploadParser {

  public UploadParser() {
  }

  public void parse(HttpServletRequest req,
                    Vector             uploadListeners,
                    int                fileSizeLimit,
                    Hashtable          parameters,
                    Hashtable          files,
                    String             tmpFolder,
                    String             encoding)
      throws IOException {
    DiskFileUpload dfu = new DiskFileUpload();
    dfu.setSizeMax(fileSizeLimit);
    dfu.setSizeThreshold(UploadConstant.MEMORY_BUFFER_SIZE);
    dfu.setRepositoryPath(tmpFolder);
    if(encoding != null) {
      dfu.setHeaderEncoding(encoding);
    }
    List list = null;
    try {
      list = dfu.parseRequest(req);
    } catch(FileUploadException fue) {
      throw new IOException(fue.getMessage());
    }
    Object obj = null;
    for(Iterator iter = list.iterator(); iter.hasNext();) {
      FileItem fi = (FileItem)iter.next();
      String fieldName = fi.getFieldName();
      if(fi.isFormField()) {
        String holder = null;
        if(encoding != null) {
          holder = fi.getString(encoding);
        } else {
          holder = fi.getString();
        }
        Vector params = (Vector)parameters.get(fieldName);
        if(params == null) {
          params = new Vector();
          parameters.put(fieldName, params);
        }
        params.addElement(holder);
      } else {
        String fin = fi.getName();

        if(fin != null) {
          UploadFileItem ufi = new UploadFileItem(fi);
          fin = fin.replace('\\', '/');
          int j = fin.lastIndexOf("/");
          if(j != -1) {
            fin = fin.substring(j + 1, fin.length());
          }
          ufi.setFileName(fin);
          ufi.setContentType(fi.getContentType());
          ufi.setFileSize(fi.getSize());
          files.put(fieldName, ufi);
        }
      }
    }
  }
}

where UploadFileItem is a simple FileItem wrapper, as in:

package com.crackwillow.app.load.upload;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.fileupload.FileItem;

import com.crackwillow.log.StdOut;

public class UploadFileItem
    implements Serializable {
  private FileItem fi;
  private String   contentType;
  private String   fileName;
  private long     fileSize;


  public UploadFileItem()                            { this.fi = null; }
  public UploadFileItem(FileItem fi)                 { this.fi = fi; }

  public FileItem getFileItem()                      { return fi; }
  public long     getFileSize()                      { return fileSize; }
  public String   getFileName()                      { return fileName; }
  public String   getContentType()                   { return contentType; }

  public void     setFileSize(long fileSize)         { this.fileSize = 
fileSize; }
  public void     setFileName(String fileName)       { this.fileName = 
fileName; }
  public void     setContentType(String contentType) { this.contentType 
= contentType; }

  public InputStream getInputStream() {
    if(fi == null) {
      return null;
    }

    InputStream is = null;
    try {
      is = new BufferedInputStream(fi.getInputStream());
    } catch (IOException ioe) {
      StdOut.log("log.error",ioe.getMessage());
    }
    return is;
  }

  public byte[] getData() {
    InputStream is = getInputStream();
    if(is != null) {
      int i = (int)getFileSize();
      if(i > 0) {
        byte data[] = new byte[i];
        try {
          is.read(data);
          is.close();
        } catch(IOException ioe) { }
        return data;
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

  public void reset() {
    if(fi != null) {
      fi.delete();
    }
  }
}

Right?

Michael McGrady



Martin Cooper wrote:

>File upload is already supported by Struts, so you don't have to worry
>about how to use Commons FileUpload yourself. If you try to do so, the
>result you are seeing is to be expected, since Struts has already
>parsed the request, and it is no longer available for your code to
>also parse.
>
>For information on how to use the file upload capabilities in Struts, see:
>
>http://wiki.apache.org/struts/StrutsFileUpload
>
>--
>Martin Cooper
>
>
>On Mon, 4 Oct 2004 10:04:16 -0400, Ivan Vasquez <ivan@itos.uga.edu> wrote:
>  
>
>>I'm trying to use commons.fileupload from struts to avoid having
>>uploaded files be part of the session.
>>
>>Following the simplest example I attempt to parsePequest but it does not
>>return any items. I verified the request and it does contains what I'm
>>expecting. The form's enctype is multipart/form-data. I'm using the
>>current release, 1.0.
>>
>>Thanks in advance.
>>Ivan
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
>
>  
>


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message