Exemple #1
0
  /** 支持multipart方式上传图片 */
  public Response multPartURL(String url, PostParameter[] params, ImageItem item, String token)
      throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
      Part[] parts = null;
      if (params == null) {
        parts = new Part[1];
      } else {
        parts = new Part[params.length + 1];
      }
      if (params != null) {
        int i = 0;
        for (PostParameter entry : params) {
          parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
        }
        parts[parts.length - 1] =
            new ByteArrayPart(item.getContent(), item.getName(), item.getContentType());
      }
      postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

      return httpRequest(postMethod, token);

    } catch (Exception ex) {
      throw new WeiboException(ex.getMessage(), ex, -1);
    }
  }
Exemple #2
0
  public Response multPartURL(
      String fileParamName,
      String url,
      PostParameter[] params,
      File file,
      boolean authenticated,
      String token)
      throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
      Part[] parts = null;
      if (params == null) {
        parts = new Part[1];
      } else {
        parts = new Part[params.length + 1];
      }
      if (params != null) {
        int i = 0;
        for (PostParameter entry : params) {
          parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
        }
      }
      FilePart filePart =
          new FilePart(
              fileParamName,
              file.getName(),
              file,
              new MimetypesFileTypeMap().getContentType(file),
              "UTF-8");
      filePart.setTransferEncoding("binary");
      parts[parts.length - 1] = filePart;

      postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
      return httpRequest(postMethod, token);
    } catch (Exception ex) {
      throw new WeiboException(ex.getMessage(), ex, -1);
    }
  }