public DownloadMaterialResponse downloadMaterial(String mediaId, MaterialType type){
        DownloadMaterialResponse response = new DownloadMaterialResponse();
        String url = BASE_API_URL + "cgi-bin/material/get_material?access_token=" + config.getAccessToken();
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(NetWorkCenter.CONNECT_TIMEOUT).setConnectTimeout(NetWorkCenter.CONNECT_TIMEOUT).setSocketTimeout(NetWorkCenter.CONNECT_TIMEOUT).build();
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        HttpPost request = new HttpPost(url);
        StringEntity mediaEntity = new StringEntity("{\"media_id\":\"" + mediaId + "\"}", ContentType.APPLICATION_JSON);
        request.setEntity(mediaEntity);

        CloseableHttpResponse httpResponse = null;
        try{
            httpResponse = client.execute(request);
            if(HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()){
                HttpEntity entity;
                String resultJson;
                switch (type){
                    case NEWS:
                        entity = httpResponse.getEntity();
                        resultJson = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                        response = JSONUtil.toBean(resultJson, DownloadMaterialResponse.class);
                        LOG.debug("-----------------请求成功-----------------");
                        LOG.debug("响应结果:");
                        LOG.debug(resultJson);
                        if (StrUtil.isBlank(response.getErrcode())) {
                            response.setErrcode("0");
                            response.setErrmsg(resultJson);
                        }
                        break;
                    case VIDEO:
                        entity = httpResponse.getEntity();
                        resultJson = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                        LOG.debug("-----------------请求成功-----------------");
                        LOG.debug("响应结果:");
                        LOG.debug(resultJson);
                        response = JSONUtil.toBean(resultJson, DownloadMaterialResponse.class);
                        if (StrUtil.isBlank(response.getErrcode())) {
                            response.setErrcode("0");
                            response.setErrmsg(resultJson);
                            // 通过down_url下载文件。文件放置在content中。通过writeTo方法获取
                            downloadVideo(response);
                        }
                        break;
                    default:
                        Header length = httpResponse.getHeaders("Content-Length")[0];
                        InputStream inputStream = httpResponse.getEntity().getContent();
                        response.setContent(inputStream, Integer.valueOf(length.getValue()));
                        break;
                }
            }else{
                response.setErrcode(String.valueOf(httpResponse.getStatusLine().getStatusCode()));
                response.setErrmsg("请求失败");
            }
        } catch (IOException e) {
            LOG.error("IO流异常", e);
        } catch (Exception e) {
            LOG.error("其他异常", e);
        }

        return response;
    }
public DownloadMaterialResponse downloadMaterial ( String mediaId , MaterialType type ) { DownloadMaterialResponse response = new DownloadMaterialResponse ( ) ; String url = BASE_API_URL + "cgi-bin/material/get_material?access_token=" + config . getAccessToken ( ) ; RequestConfig config = RequestConfig . custom ( ) . setConnectionRequestTimeout ( NetWorkCenter . CONNECT_TIMEOUT ) . setConnectTimeout ( NetWorkCenter . CONNECT_TIMEOUT ) . setSocketTimeout ( NetWorkCenter . CONNECT_TIMEOUT ) . build ( ) ; CloseableHttpClient client = HttpClientBuilder . create ( ) . setDefaultRequestConfig ( config ) . build ( ) ; HttpPost request = new HttpPost ( url ) ; StringEntity mediaEntity = new StringEntity ( "{\"media_id\":\"" + mediaId + "\"}" , ContentType . APPLICATION_JSON ) ; request . setEntity ( mediaEntity ) ; CloseableHttpResponse httpResponse = null ; try { httpResponse = client . execute ( request ) ; if ( HttpStatus . SC_OK == httpResponse . getStatusLine ( ) . getStatusCode ( ) ) { HttpEntity entity ; String resultJson ; switch ( type ) { case NEWS : entity = httpResponse . getEntity ( ) ; resultJson = EntityUtils . toString ( entity , Charset . forName ( "UTF-8" ) ) ; response = JSONUtil . toBean ( resultJson , DownloadMaterialResponse . class ) ; LOG . debug ( "-----------------请求成功-----------------" ) ; LOG . debug ( "响应结果:" ) ; LOG . debug ( resultJson ) ; if ( StrUtil . isBlank ( response . getErrcode ( ) ) ) { response . setErrcode ( "0" ) ; response . setErrmsg ( resultJson ) ; } break ; case VIDEO : entity = httpResponse . getEntity ( ) ; resultJson = EntityUtils . toString ( entity , Charset . forName ( "UTF-8" ) ) ; LOG . debug ( "-----------------请求成功-----------------" ) ; LOG . debug ( "响应结果:" ) ; LOG . debug ( resultJson ) ; response = JSONUtil . toBean ( resultJson , DownloadMaterialResponse . class ) ; if ( StrUtil . isBlank ( response . getErrcode ( ) ) ) { response . setErrcode ( "0" ) ; response . setErrmsg ( resultJson ) ; downloadVideo ( response ) ; } break ; default : Header length = httpResponse . getHeaders ( "Content-Length" ) [ 0 ] ; InputStream inputStream = httpResponse . getEntity ( ) . getContent ( ) ; response . setContent ( inputStream , Integer . valueOf ( length . getValue ( ) ) ) ; break ; } } else { response . setErrcode ( String . valueOf ( httpResponse . getStatusLine ( ) . getStatusCode ( ) ) ) ; response . setErrmsg ( "请求失败" ) ; } } catch ( IOException e ) { LOG . error ( "IO流异常" , e ) ; } catch ( Exception e ) { LOG . error ( "其他异常" , e ) ; } return response ; }
下载永久素材
@param mediaId 素材ID
@param type 素材类型
@return 下载结果