commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dakota Jack <dakota.j...@gmail.com>
Subject Re: FileUpoad:
Date Thu, 21 Apr 2005 00:09:20 GMT
Well, if you want the id earlier, get it earlier?  Also, there are all
sorts of things you can do by extending the commons classes, e.g. I
drop a monitor in as follows:

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;
  }
}

And this allows stuff like follows:


    UploadFileItemFactory ufiFactory = new UploadFileItemFactory();

    ufiFactory.setMonitors(monitors);
    ufiFactory.setContentLength(req.getContentLength());

    DiskFileUpload dfu = new DiskFileUpload();

    dfu.setFileItemFactory(ufiFactory);
    dfu.setSizeMax(maxFileSize);
    dfu.setSizeThreshold(Upload.BUFFER_SIZE);
    dfu.setRepositoryPath(repositoryPath);

    if(encoding != null) {
      dfu.setHeaderEncoding(encoding);
    }

    List list = null;

    try {
      list = dfu.parseRequest(req);
    } catch(FileUploadException cfue) {
      throw new IOException(cfue.getMessage());
    }

    Object obj = null;

    for(Iterator iterator = list.iterator(); iterator.hasNext();) {
        FileItem fi        = (FileItem)iterator.next();
        String   fieldName = fi.getFieldName();

      if(fi.isFormField()) {
        String data = null;
        if(encoding != null) {
          data = fi.getString(encoding);
        } else {
          data = fi.getString();
        }

        List names = (List)parameterNames.get(fieldName);

        if(names == null) {
          names =  Collections.synchronizedList(new LinkedList());
          parameterNames.put(fieldName, names);
        }

        names.add(data);
      } else {
        String name        = fi.getName();
        String ext         = name;
        String contentType = null;
        int    flag        = -99;
        if(name != null) {
          MultipartFile mf = new UploadMultipartFile(fi);
          mf.setName(name);
          mf.setContentType(contentType = fi.getContentType());
          mf.setSize(fi.getSize());
          files.put(fieldName, mf);

          //---------------Take care of mime map for downloads------------------
          flag = ext.lastIndexOf('.');
          ext  = ext.substring(flag + 1);
          if(flag != -1 && "".equals(mime_map.getProperty(ext))) {
            mime_map.setProperty(ext,contentType);
            LoadMimes.setMimes(mime_map);
          }
          //--------------------------------------------------------------------
        }
      }
    }
  }

Get what I mean?

On 4/20/05, Tom Eugelink <tbee@tbee.org> wrote:
> > No, don't agree.  Show me the code that you use to farm the data.  I
> > assume you are using Commons fileupload.  But, how you do it is what
> > makes a difference.  This stuff about the position on the form can be
> > discussed later.  No sense letting a great philosophical discussion
> > get in the way of getting something done.  ///;-)
> 
> Agree.
> 
>         // Check that we have a file upload request
>         if (!FileUpload.isMultipartContent(request))
>         {
>                         ServletException lServletException = new ServletException("Only
file
> uploads are accepted to this URL");
>                         log4j.error(lServletException);
>                         throw lServletException;
>         }
> 
>         // Create a new file upload handler
>         DiskFileUpload lFileUpload = new DiskFileUpload();
>         lFileUpload.setSizeThreshold(iMaxSize);
>         lFileUpload.setSizeMax(iMaxSize);
>         lFileUpload.setRepositoryPath(iTmpDir.getAbsolutePath());
> 
>         //
>         // Parse the request
>         List lFileItems = null;
>         try
>                 {
>                 lFileItems = lFileUpload.parseRequest(request);
>                 }
>         catch (FileUploadException e)
>                 {
>                 response.sendRedirect(iOnError + "?error=parse&info=Bestand is te
> groot");
>                 return;
>                 }
> 
>         //
>         // get the ID
>         FileItem lIdFileItem = getFileItemFromList(lFileItems, "id");
> 
> As you can see the sendRedirect occurs before I get the id...
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message