Search in sources :

Example 1 with WeixinMedia

use of com.guhanjie.weixin.model.WeixinMedia in project weixin-boot by guhanjie.

the class MediaKit method postMedia.

public static String postMedia(String path, String type) {
    LOGGER.debug("starting to post media: path[{}] type[{}]...", path, type);
    final WeixinMedia wm = new WeixinMedia();
    try {
        String url = WeixinConstants.API_POST_MEDIA;
        url = url.replace("TYPE", type);
        FileBody fb = new FileBody(new File(path));
        HttpEntity entity = MultipartEntityBuilder.create().addPart("media", fb).build();
        WeixinHttpUtil.sendPost(url, entity, new WeixinHttpCallback() {

            @Override
            public void process(String json) {
                WeixinMedia t = JSONObject.parseObject(json, WeixinMedia.class);
                if (t != null && t.getMedia_id() != null) {
                    wm.setMedia_id(t.getMedia_id());
                    LOGGER.info("Success to post media:[{}].", t.getMedia_id());
                } else {
                    LOGGER.error("Failed to post media.");
                }
            }
        });
    } catch (Exception e) {
        LOGGER.error("error post media: type[{}], path[{}]", type, path);
    }
    return wm.getMedia_id();
}
Also used : WeixinMedia(com.guhanjie.weixin.model.WeixinMedia) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) File(java.io.File) WeixinHttpCallback(com.guhanjie.weixin.WeixinHttpUtil.WeixinHttpCallback)

Aggregations

WeixinHttpCallback (com.guhanjie.weixin.WeixinHttpUtil.WeixinHttpCallback)1 WeixinMedia (com.guhanjie.weixin.model.WeixinMedia)1 File (java.io.File)1 HttpEntity (org.apache.http.HttpEntity)1 FileBody (org.apache.http.entity.mime.content.FileBody)1