org.nutz.http.sender.FilePostSender Java Examples

The following examples show how to use org.nutz.http.sender.FilePostSender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpTool.java    From mpsdk4j with Apache License 2.0 6 votes vote down vote up
public static String upload(String url, File file) {
    if (log.isDebugEnabled()) {
        log.debugf("Upload url: %s, file name: %s, default timeout: %d",
                   url,
                   file.getName(),
                   CONNECT_TIME_OUT);
    }

    try {
        Request req = Request.create(url, METHOD.POST);
        req.getParams().put("media", file);
        Response resp = new FilePostSender(req).send();
        if (resp.isOK()) {
            String content = resp.getContent();
            return content;
        }

        throw Lang.wrapThrow(new RuntimeException(String.format("Upload file [%s] failed. status: %d",
                                                                url,
                                                                resp.getStatus())));
    }
    catch (Exception e) {
        throw Lang.wrapThrow(e);
    }
}
 
Example #2
Source File: WxApiImpl.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
@Override
public String mediaUpload(String type, File f) {
    if (type == null)
        throw new NullPointerException("media type is NULL");
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("http://file.api.weixin.qq.com/cgi-bin/media/upload?token=%s&type=%s",
                               getAccessToken(),
                               type);
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("media upload file, resp code=" + resp.getStatus());
    Map<String, Object> map = (Map<String, Object>) Json.fromJson(resp.getReader());
    if (map != null
        && map.containsKey("errcode")
        && ((Number) map.get("errcode")).intValue() != 0) {
        throw new IllegalArgumentException(map.toString());
    }
    return map.get("media_id").toString();
}
 
Example #3
Source File: WxApi2Impl.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
@Override
public WxResp add_video(File f, String title, String introduction) {
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("%s/cgi-bin/material/add_material?type=video&access_token=%s",
                               wxBase,
                               getAccessToken());
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    req.getParams()
       .put("description",
            Json.toJson(new NutMap().setv("title", title).setv("introduction", introduction),
                        JsonFormat.compact().setQuoteName(true)));
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("add_material, resp code=" + resp.getStatus());
    return Json.fromJson(WxResp.class, resp.getReader("UTF-8"));
}
 
Example #4
Source File: WxApi2Impl.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@Override
public WxResp media_upload(String type, File f) {
    if (type == null)
        throw new NullPointerException("media type is NULL");
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("%s/cgi-bin/media/upload?access_token=%s&type=%s", wxBase, getAccessToken(), type);
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("media upload file, resp code=" + resp.getStatus());
    return Json.fromJson(WxResp.class, resp.getReader("UTF-8"));
}
 
Example #5
Source File: WxApi2Impl.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@Override
public WxResp uploadimg(File f) {
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("%s/cgi-bin/media/uploadimg?access_token=%s", wxBase, getAccessToken());
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("uploadimg, resp code=" + resp.getStatus());
    return Json.fromJson(WxResp.class, resp.getReader("UTF-8"));
}
 
Example #6
Source File: WxApi2Impl.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@Override
public WxResp add_material(String type, File f) {
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("%s/cgi-bin/material/add_material?access_token=%s&type=%s",
                               wxBase,
                               getAccessToken(),
                               type);
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("add_material, resp code=" + resp.getStatus());
    return Json.fromJson(WxResp.class, resp.getReader("UTF-8"));
}
 
Example #7
Source File: WxApi2Impl.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@Override
public WxResp kfaccount_uploadheadimg(String kf_account, File f) {
    if (f == null)
        throw new NullPointerException("meida file is NULL");
    String url = String.format("%s/customservice/kfaccount/uploadheadimg?access_token=%s",
                               wxBase,
                               getAccessToken());
    Request req = Request.create(url, METHOD.POST);
    req.getParams().put("media", f);
    Response resp = new FilePostSender(req).send();
    if (!resp.isOK())
        throw new IllegalStateException("uploadimg, resp code=" + resp.getStatus());
    return Json.fromJson(WxResp.class, resp.getReader("UTF-8"));
}
 
Example #8
Source File: SenderInterceptorTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Test
public void test_filepostsender_send() throws NoSuchMethodException, SecurityException, Throwable {
    setupSender(FilePostSender.class);
    _sender_sender_test();
}