Java 类org.apache.commons.httpclient.methods.MultipartPostMethod 实例源码

项目:http4e    文件:BusinessJob.java   
private void addMultipart_Parts( MultipartPostMethod method, Map<String, String> parameterizedMap){
   for (Iterator it = model.getParameters().entrySet().iterator(); it.hasNext();) {
      Map.Entry me = (Map.Entry) it.next();
      String key = (String) me.getKey();
      List values = (List) me.getValue();
      StringBuilder sb = new StringBuilder();
      int cnt = 0;
      for (Iterator it2 = values.iterator(); it2.hasNext();) {
         String val = (String) it2.next();
         if (cnt != 0) {
            sb.append(",");
         }
         sb.append(val);
         cnt++;
      }

      String parameterizedVal = ParseUtils.getParametizedArg(sb.toString(), parameterizedMap);

      if (parameterizedVal.startsWith("@")) {
         String path = "";
         String contentType = "";
         try {
            path = parameterizedVal.substring(1, parameterizedVal.length()).trim();
            path = path.replace('\\', '/');
            contentType = getContentType(getFileExt(path));
            File f = new File(path);
            method.addPart(new FilePart(key, f, contentType, null));
            // postMethod.addParameter("file", f);
            // postMethod.addPart(new FilePart("file", f));
         } catch (FileNotFoundException fnfe) {
            ExceptionHandler.handle(fnfe);
         }

      } else {
         method.addPart(new StringPart(key, parameterizedVal));

      }
   }
}
项目:OSCAR-ConCert    文件:ManageEFormAction.java   
public ActionForward exportEFormSend(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response) throws Exception {

    if(!securityInfoManager.hasPrivilege(LoggedInInfo.getLoggedInInfoFromSession(request), "_eform", "r", null)) {
    throw new SecurityException("missing required security object (_eform)");
}


      String username = request.getParameter("username");
      String password = request.getParameter("password");

      String fid = request.getParameter("fid");
      MiscUtils.getLogger().debug("fid: " + fid);
      EForm eForm = new EForm(fid, "1");
      //===================
      HttpClient client = new HttpClient();
      client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

      PostMethod method = new PostMethod("http://know2act.org/sessions");



      method.addParameter("session[email]", username);
      method.addParameter("session[password]", password);

      int statusCode = client.executeMethod(method);

      //need to check if login worked

      byte[] responseBody = method.getResponseBody();

      MiscUtils.getLogger().debug(new String(responseBody));



      MiscUtils.getLogger().debug("--------------------------------------------------------------------------------------");
       MultipartPostMethod eformPost = new MultipartPostMethod("http://know2act.org/e_forms/");

      String documentDir = OscarProperties.getInstance().getProperty("DOCUMENT_DIR");
      File docDir = new File(documentDir);
      String exportFilename = "eformExport"+System.currentTimeMillis()+""+(Math.random()*100);
      MiscUtils.getLogger().debug("Exported file name "+exportFilename);
      File exportFile = new File(documentDir,exportFilename);

      FileOutputStream fos = new FileOutputStream(exportFile);

      EFormExportZip eFormExportZip = new EFormExportZip();
      List<EForm> eForms = new ArrayList<EForm>();
      eForms.add(eForm);
      eFormExportZip.exportForms(eForms, fos);
      fos.close();

      eformPost.addParameter("e_form[name]", eForm.getFormName());
      eformPost.addParameter("e_form[category]", request.getParameter("category"));
      eformPost.addParameter("e_form[uploaded_data]", exportFile.getName(), exportFile);


      int statusCode2 = client.executeMethod(eformPost);

      byte[] responseBody2 = eformPost.getResponseBody();

      MiscUtils.getLogger().debug("ST " + statusCode2);
      MiscUtils.getLogger().debug(new String(responseBody2));
      //TODO:Need to handle errors



      return mapping.findForward("success");
  }
项目:oscar-old    文件:ManageEFormAction.java   
public ActionForward exportEFormSend(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {

    String username = request.getParameter("username");
    String password = request.getParameter("password");

    String fid = request.getParameter("fid");
    MiscUtils.getLogger().debug("fid: " + fid);
    EForm eForm = new EForm(fid, "1");
    //===================
    HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

    PostMethod method = new PostMethod("http://mydrugref.org/sessions");



    method.addParameter("session[email]", username);
    method.addParameter("session[password]", password);

    int statusCode = client.executeMethod(method);

    //need to check if login worked

    byte[] responseBody = method.getResponseBody();

    MiscUtils.getLogger().debug(new String(responseBody));



    MiscUtils.getLogger().debug("--------------------------------------------------------------------------------------");
     MultipartPostMethod eformPost = new MultipartPostMethod("http://mydrugref.org/e_forms/");

    String documentDir = OscarProperties.getInstance().getProperty("DOCUMENT_DIR");
    File docDir = new File(documentDir);
    String exportFilename = "eformExport"+System.currentTimeMillis()+""+(Math.random()*100);
    MiscUtils.getLogger().debug("Exported file name "+exportFilename);
    File exportFile = new File(documentDir,exportFilename);

    FileOutputStream fos = new FileOutputStream(exportFile);

    EFormExportZip eFormExportZip = new EFormExportZip();
    List<EForm> eForms = new ArrayList<EForm>();
    eForms.add(eForm);
    eFormExportZip.exportForms(eForms, fos);
    fos.close();

    eformPost.addParameter("e_form[name]", eForm.getFormName());
    eformPost.addParameter("e_form[category]", request.getParameter("category"));
    eformPost.addParameter("e_form[uploaded_data]", exportFile.getName(), exportFile);


    int statusCode2 = client.executeMethod(eformPost);

    byte[] responseBody2 = eformPost.getResponseBody();

    MiscUtils.getLogger().debug("ST " + statusCode2);
    MiscUtils.getLogger().debug(new String(responseBody2));
    //TODO:Need to handle errors



    return mapping.findForward("success");
}