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: [FileUpload] Cannot extend DefaultFileItem
Date Thu, 25 Nov 2004 08:18:13 GMT
Don't know if you are interested, Jason, but I do something like the
following (follow the bouncing ball):


public class UploadAction
    extends Action {
  public ActionForward execute(ActionMapping       mapping,
                               ActionForm          form,
                               HttpServletRequest  request,
                               HttpServletResponse response)
      throws Exception {
    HttpSession            session    = request.getSession();
    ProgressUploadListener listener = null;

    if((listener =
(ProgressUploadListener)session.getAttribute(UploadConstant.LISTENER))
== null) {
      listener = new ProgressUploadListener();
      session.setAttribute(UploadConstant.LISTENER,listener);
    }

    if(UploadMultipartFormDataRequest.isMultipartFormData(request)) {
      Upload upload = null;

      if((upload =
(Upload)session.getAttribute(UploadConstant.UPLOAD)) == null) {
        upload = new Upload();
        session.setAttribute(UploadConstant.UPLOAD,upload);
      }

      upload.setFolder(UploadConstant.UPLOADS_FOLDER);
      upload.setParserTempDir(UploadConstant.UPLOADS_TEMP_FOLDER);
      upload.addUploadListener(listener);
      request.getSession().setAttribute(UploadConstant.LISTENER,listener);

      Vector listeners = new Vector();

      listeners.addElement(listener);

      UploadMultipartFormDataRequest umfdr  = new
UploadMultipartFormDataRequest(request,listeners);
      String                         method = null;

      if(umfdr != null) {
        Enumeration enum        = umfdr.getParameterNames();
        String      buttonValue = null;

        while(enum.hasMoreElements()) {
          buttonValue = (String)enum.nextElement();
          if(buttonValue.indexOf(ButtonConstant.BUTTON_DISPATCH) != -1) {
            method = buttonValue.substring(0,buttonValue.indexOf('.'));
            break;
          }
        }
      }

      if((method != null) && (UploadConstant.UPLOAD.equals(method))) {
        upload.store(umfdr);
        Vector history = upload.getHistory();
        StringBuffer sb = new StringBuffer("<ul>");

        for(int i = 0; i < history.size(); i++) {
          UploadParams up = (UploadParams) history.elementAt(i);
          sb.append("<li><pre>File Name    : "+ up.getFileName() + "
(" + up.getFileSize()+" bytes)");
          sb.append("<BR>Content Type : " + up.getContentType());
          sb.append("<BR>Storage      : " + up.getStoreModelName() +"
(" + up.getStoreInfo() + ")</pre>");
        }

        sb.append("</ul>");
        session.setAttribute("report",sb.toString());
      } else {
        StdOut.log("log.trace","UploadAction method is null or not upload");
      }
    }

    ActionForward    forward = null;
    String           path    = mapping.getParameter();
    String           layout  = null;
    HostKeyContainer hkc     =
((HostKeyContainer)request.getSession().getAttribute(SiteConstant.HOST_KEY_CONTAINER));

    if(hkc == null) {
      hkc  = HostKeyContainerFactory.getWebmasterHostKeyContainer();
      layout = (String)hkc.get(SiteConstant.LAYOUT);
      forward = new ActionForward(layout + "_" + path);
    } else {
      layout = (String)hkc.get(SiteConstant.LAYOUT);
      forward = new ActionForward(layout + "_" + path);
    }

    return forward;
  }
}

Then ====================================


  public UploadMultipartFormDataRequest(HttpServletRequest req,
                                        Vector             listeners,
                                        int                fileSizeLimit,
                                        String             encoding)
      throws UploadException,
             IOException {
    this.parameterNames = null;
    this.files          = null;
    this.parameterNames = new Hashtable();
    this.files          = new Hashtable();

    if(req == null) {
      new UploadException(UploadConstant.INVALID_REQUEST);
    }

    Multipart pm = new Multipart();

    pm.handleRequest(req, listeners, fileSizeLimit,
this.parameterNames, this.files, UploadConstant.PARSER_TEMP_DIR,
encoding);

  }

then ===========================================


public class Multipart {

  public Multipart() {
  }

  public void handleRequest(HttpServletRequest req,
                            Vector             listeners,
                            int                maxFileSize,
                            Hashtable          parameterNames,
                            Hashtable          files,
                            String             repositoryPath,
                            String             encoding)
      throws IOException {

    FileItemFactory fufiFactory = new FileItemFactory();

    fufiFactory.setListeners(listeners);
    fufiFactory.setCustom(req.getContentLength());

    DiskFileUpload cdfu = new DiskFileUpload();

    cdfu.setFileItemFactory(fufiFactory);
    cdfu.setSizeMax(maxFileSize);
    cdfu.setSizeThreshold(UploadConstant.BUFFER_SIZE);
    cdfu.setRepositoryPath(repositoryPath);

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

    List list = null;

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

    Object obj = null;

    for(Iterator iterator = list.iterator(); iterator.hasNext();) {

      FileItem cfi       = (FileItem)iterator.next();
      String          fieldName = cfi.getFieldName();

      if(cfi.isFormField()) {
        String s3 = null;
        if(encoding != null) {
          s3 = cfi.getString(encoding);
        } else {
          s3 = cfi.getString();
        }

        Vector vector = (Vector)parameterNames.get(fieldName);

        if(vector == null) {
          vector = new Vector();
          parameterNames.put(fieldName, vector);
        }

        vector.addElement(s3);
      } else {
        String name = cfi.getName();
        if(name != null) {
          UploadFile fuuf = new UploadFile(cfi);
          name = name.replace('\\', '/');
          int j = name.lastIndexOf("/");

          if(j != -1) {
            name = name.substring(j + 1, name.length());
          }

          fuuf.setFileName(name);
          fuuf.setContentType(cfi.getContentType());
          fuuf.setFileSize(cfi.getSize());
          files.put(fieldName, fuuf);
        }
      }
    }

  }
}

You get the idea, right?  This is all with the commons classes without
the default.

Jack

-- 


"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

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