Java micro-channel public platform for the development of material management (Spring Boot 2.X)

Micro-channel bulk material management and this document very friendly Java. This article is only for new temporary material, new material made permanent introduction, the rest get, delete, modify, supplement their own

No public often need to use some temporary scenes multimedia material, for example in the use of the interface in particular, to send a message, the operation of multimedia files, access and call multimedia messages, etc., are carried out by the media_id. Material management interface for all certified subscription number and service number open.

Restrictions clip

Pictures (image): 2M, support the PNG \ JPEG \ JPG \ GIF format
Voice (voice): 2M, players no longer than 60s, supports AMR \ MP3 format
Video (video): 10MB, support MP4 format
thumbnail (thumb): 64KB, JPG format support

First, the new temporary material

Interface: https: //api.weixin.qq.com/cgi-bin/media/upload access_token = ACCESS_TOKEN & type = TYPE, re-transmission of a media file types, can be a picture (image), voice (voice), video (video? ) and thumbnail (thumb).

1, subscription number and service number to be authenticated by
2, media_id temporary material is reusable
3, the media files stored in the micro letter backstage time is 3 days, or 3 days media_id failure.

/ ** 
     * Added temporary material (local) 
     * 
     * @param accessToken 
     * @param of the type of media file types, respectively picture (image), voice (voice), (video) and thumbnail (Thumb) 
     * @param filePath local path 
     * @return 
     * / 
    @Override 
    public MaterialResult uploadTempMediaFile (accessToken String, String type, String filePath) { 
        the Map <String, String> the params = new new the HashMap <> (); 
        params.put ( "the access_token", accessToken); 
        the params. PUT ( "type", type); 
        the try { 
            String Result = MaterialUtil.uploadMediaFile (wechatMaterialConfig.getUploadTempMediaUrl (), the params, filePath);  
            return JsonUtil.fromJson (Result, MaterialResult.class);
        } the catch (Exception E) { 
            log.error (E.getMessage());
        } 
        Return null; 
    } 
 
    / ** 
     * new temporary material (network) 
     * 
     * @param accessToken 
     * @param of the type of media file types, respectively picture (image), voice (voice), (video) and thumbnail (thumb) 
     * @param url network path 
     * @return 
     * / 
    @Override 
    public MaterialResult uploadTempMediaUrl (accessToken String, String type, String URL) { 
        the Map <String, String> the params = new new the HashMap <> ();  
        the params .put ( "the access_token", accessToken); 
        params.put ( "type", type); 
        the try {
            String Result = MaterialUtil.uploadMediaUrl (wechatMaterialConfig .getUploadTempMediaUrl (), the params, URL); 
            return JsonUtil.fromJson (Result, MaterialResult.class); 
        } the catch (Exception E) {
            log.error(e.getMessage());
        }
        return null;
    }

Second, the new permanent material

Interface: https:? //Api.weixin.qq.com/cgi-bin/material/add_material access_token = ACCESS_TOKEN & type = TYPE, media file types, respectively picture (image), voice (voice), video (video, exceptions) and thumbnail (thumb)

/ ** 
     * new permanent material (local) 
     * 
     * @param accessToken 
     * @param of the type of media file types, respectively picture (image), voice (voice) and thumbnail (Thumb) 
     * @param filePath local path 
     * @return 
     * / 
    @Override 
    public MaterialResult uploadForeverMediaFile (accessToken String, String type, String filePath) { 
        the Map <String, String> the params = new new the HashMap <> (); 
        params.put ( "the access_token", accessToken); 
        params.put ( "type ", type); 
        the try { 
            String Result = MaterialUtil.uploadMediaFile (wechatMaterialConfig.getUploadForeverMediaUrl (), the params, filePath); 
            return JsonUtil.fromJson (Result, MaterialResult.class); 
        } the catch (Exception E) { 
            log.error (e.getMessage ());
        } 
        Return null; 
    } 
 
 
    / ** 
     * new permanent material (net) 
     * 
     * @param accessToken 
     * @param of the type of media file types, respectively picture (image), voice (voice) and thumbnail (Thumb) 
     * @param url network path 
     * @return 
     * / 
    @Override 
    public MaterialResult uploadForeverMediaUrl (accessToken String, String type, String URL) { 
        the Map <String, String> the params = new new the HashMap <> ();  
        params.put ( " the access_token ", accessToken); 
        params.put (" type ", type); 
        the try {
            String Result = MaterialUtil.uploadMediaUrl (wechatMaterialConfig.getUploadForeverMediaUrl () , the params, URL); 
            return JsonUtil.fromJson (Result, MaterialResult.class); 
        } the catch (Exception E) {
            log.error(e.getMessage());
        }
        return null;
    }

New video material permanently in need of special attention to the need POST another form when uploading video clips, id for the description, the description contains material information title and introduction, content format as JSON

/ ** 
     * Upload permanent local video creative 
     * Back format 
     * { 
     * "media_id": media_id 
     *} 
     * 
     * @param accessToken 
     * @param title title of the video clip 
     * @param introduction described video material 
     * @param filePath local path 
     * @return 
     * / 
    @Override 
    public String uploadForeverMediaFile (accessToken String, String title, Introduction String, String filePath) { 
        // only mp4 of 
        the Map <String, String> the params = new new the HashMap <> (); 
        params.put ( " the access_token ", accessToken); 
        params.put (" of the type ", WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_VIDEO); 
        the try {
            Result = MaterialUtil.uploadVideoMediaFile String (wechatMaterialConfig.getUploadForeverMediaUrl (), 
                    the params, filePath, title, Introduction); 
            return JsonUtil.fromJson (Result, MaterialResult.class) .getMediaId (); 
        } the catch (Exception E) { 
            log.error (E .getMessage ()); 
        } 
        return null; 
    } 
 
    / ** 
     * Upload permanent network video creative 
     * Back format 
     * { 
     * "media_id": media_id 
     *} 
     * 
     * @param accessToken 
     @param * title the title of the video material 
     * @param introduction description of the video material 
     * @param url network path 
     * @return 
     * /
    @Override
    public String uploadForeverMediaUrl(String accessToken, String title, String introduction, String url) {
        //只能是mp4的
        Map<String, String> params = new HashMap<>();
        params.put("access_token", accessToken);
        params.put("type", WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_VIDEO);
        try {
            String result = MaterialUtil.uploadVideoMediaUrl(wechatMaterialConfig.getUploadForeverMediaUrl(),
                    params, url, title, introduction);
//            log.error(result);
            return JsonUtil.fromJson(result, MaterialResult.class).getMediaId();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

Third, the new permanent graphic material

Interface: https:? //Api.weixin.qq.com/cgi-bin/material/add_news access_token = ACCESS_TOKEN
 for commonly used materials, developers can upload through the port to the micro-channel server, permanently.
1, a new permanent picture material after the increase, with the URL returned to the developer, the developer can be used within the system Tencent domain name (domain name system Tencent external use, the picture will be masked).
2, number of public library materials Total capped save: graphic news material, the upper limit is 5000 picture material, other types of 1000.
3, the specific content of the graphic message, micro letter backstage to filter external image links, images url need through the "upload pictures in the graphic message retrieval URL" interface to upload pictures to obtain.
4, "upload pictures in the graphic message retrieval URL" interface to upload pictures, do not take 5000 limited number of public Library in number of images, pictures only supports jpg / png format and size must be below 1MB.
5, graphic text messages to support their ability to insert account number and other public has mass article link.

/ ** 
     * permanent new creative graphics 
     * Returning to the example 
     * { 
     * "media_id": media_id 
     *} 
     * live new creative graphic message media_id 
     * 
     * @param accessToken 
     * @param Entity combined graphic message article Data 
     * @return 
     * @see WechatMassService uploadForMassNewsFile # 
     * / 
    @Override 
    public uploadNewsMedia String (String accessToken, UploadNewsEntity Entity) { 
        the Map <String, String> the params = new new the HashMap <> (); 
        params.put ( "the access_token", accessToken) ; 
        String Data = JsonUtil.toJson (Entity);  
        String Result = HttpUtil.doPost (wechatMaterialConfig.getAddNewsUrl (), the params, Data);
        return JsonUtil.fromJson (Result, MassNewsResult.class) .getMediaId (); 
    }

Fourth, the picture in the message retrieval URL Upload Photo 

Interface: https: //api.weixin.qq.com/cgi-bin/media/uploadimg access_token = ACCESS_TOKEN?

This interface to upload images do not occupy 5000 limited number of public Library in number of images. Pictures only supports jpg / png format, size must be 1MB or less, this interface is returned url upload pictures URL, graphic messages can be placed in use.

  / ** 
     * { 
     * "url": "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ / 0" 
     *} 
     * details graphic message, the micro-channel filtering external background image links, image url need to "obtain the URL of the uploaded graphic images within messages" through the interface to upload pictures to obtain 
     * get 
     * 
     * @param accessToken 
     * @param filePath local path 
     * @return 
     * / 
    @Override 
    public String uploadFileForNewsMedia (accessToken String, String filePath) { 
        the Map < String, String> the params = new new the HashMap <> (); 
        params.put ( "the access_token", accessToken); 
        the try {
            Result = MaterialUtil.uploadMediaFile String (wechatMaterialConfig.getUploadimgMediaUrl (), the params, filePath); 
            return JsonUtil.fromJson (Result, MaterialResult.class) .getUrl (); 
        } the catch (Exception E) { 
            log.error (e.getMessage () ); 
        } 
        return null; 
    } 
 
    / ** 
     * { 
     * "URL": "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ / 0" 
     *} 
     * details graphic message, micro-channel background filter external image links, images url need through the "upload pictures in the graphic message retrieval uRL" interface to upload pictures to obtain 
     * get 
     * 
     * @param accessToken 
     * @param url network path 
     * @return
     */
    @Override
    public String uploadUrlForNewsMedia(String accessToken, String url) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", accessToken);
        try {
            String result = MaterialUtil.uploadMediaUrl(wechatMaterialConfig.getUploadimgMediaUrl(), params, url);
            return JsonUtil.fromJson(result, MaterialResult.class).getUrl();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

Five, Junit test

 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WechatMainApplication.class)
@ActiveProfiles("dev")
public class WechatMaterialServiceImplTest {
 
    @Autowired
    private WechatAuthService wechatAuthService;
 
    @Autowired
    private WechatMaterialService wechatMaterialService;
 
    @Test
    public void uploadTempMediaFile() {
        String filePath = "C:/Users/Phil/Pictures/cc0a42661f668d17e18d0a0f3a699909.jpeg";
        String accessToken = wechatAuthService.getAccessToken();
        System.out.println("新增临时本地图片素材,返回" + wechatMaterialService.uploadTempMediaFile(accessToken,
                WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_IMAGE, filePath)); 
        // add a temporary local picture material, return MassNewsResult (Image type =, = rh0D9L_B5Ty_ mediaId-EcwIFULGjb57eCGAbQZRz8bNrWnfcLMiMz2Pm7vIQCdUKzC8C_8, createdAt = 1543648966, URL = null) 
    } 
 
    @Test 
    public void uploadTempMediaUrl () { 
        String URL = "http://b-ssl.duitang.com/uploads/item/201503/14/20150314212812_kCLmy.jpeg"; 
        String accessToken = wechatAuthService.getAccessToken (); 
        System.out.println ( "new temporary network picture material, return "+ wechatMaterialService.uploadTempMediaUrl (accessToken, 
                WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_IMAGE, URL));
        // add temporary network picture material, return MassNewsResult (of the type = Image, mediaId = lgbD-K0ltDTmsW2dSxus6sFxqqFc3mU_NiIAOev_U_mU2Yqa6iSv8KPF5e5WE7yC, createdAt = 1543648973, url = null) 
    } 
 
    @Test 
    public void uploadForeverMediaFile () { 
        String filePath = "C: / the Users / Phil / pictures / cc0a42661f668d17e18d0a0f3a699909.jpeg "; 
        String accessToken = wechatAuthService.getAccessToken (); 
        System.out.println (" new permanent local picture material, return "+ wechatMaterialService.uploadForeverMediaFile (accessToken, 
                WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_IMAGE, filePath));
        // add a permanent local picture material, return MassNewsResult (of the type = null, mediaId = tbP52CbH1vuCLlyUAt2rlUEZKK230SjWIaB9xablAPw, createdAt = 0, url = HTTP:? //Mmbiz.qpic.cn/mmbiz_jpg/L8zibbZGicwDyd6CV9JpbEe6T7rjqK2fBibCrZAxEvwDCJKHAgtfUialcnNlcjxTsAmycHSKjrO6KzdDUMeDwhYsIA/0 wx_fmt = jpeg) 
    } 
 
    @Test 
    public uploadForeverMediaUrl void () { 
        String URL = "http://www.59xihuan.cn/uploads/allimg/201309/9181378183464-lp.jpg"; 
// String URL = "http://b-ssl.duitang.com/ uploads / Item / 201503/14 / 20150314212812_kCLmy.jpeg "; 
        String accessToken = wechatAuthService.getAccessToken (); 
        System.out.println (" new permanent network picture material, return "+ wechatMaterialService.uploadForeverMediaUrl (accessToken,
                WechatMaterialConstant.MATERIAL_UPLOAD_TYPE_IMAGE, url));
        //新增永久网络图片素材, 返回MassNewsResult(type=null, mediaId=GG16CzQSj3di2OTScAcNAzmoxg_5_KTyxsJ6uEEBpiM, createdAt=0, url=http://mmbiz.qpic.cn/mmbiz_jpg/3WjFdhSpzNwwEwTpWMU4j8UVddOSDGR2zRuJsQZXaRYnsIB3g4yESBqjxkf1e7ruIWYfUO5FtRWIibaVwt6icOHw/0?wx_fmt=jpeg)
    }
 
    @Test
    public void uploadNewsMedia() {
        String accessToken = wechatAuthService.getAccessToken();
        UploadNewsEntity entity = new UploadNewsEntity();
        UploadNewsEntity.UploadNewsArticle article1 = new UploadNewsEntity.UploadNewsArticle();
        article1.setThumbMediaId("tbP52CbH1vuCLlyUAt2rlWEIB6qGsfypnI01Jwwonjg");
        article1.setAuthor("article1");
        article1.setTitle("article1");
        article1.setContentSourceUrl("");
        article1.setContent("article1");
        article1.setDigest("article1");
        article1.setShowConverPic(1);
        entity.addArticle(article1);
        UploadNewsEntity.UploadNewsArticle article2 = new UploadNewsEntity.UploadNewsArticle();
        article2.setThumbMediaId("tbP52CbH1vuCLlyUAt2rlRjbl4sO2dPYqvCLZNwIorg");
        article2.setAuthor("article2");
        article2.setTitle("article2");
        article2.setContentSourceUrl("");
        article2.setContent("article2");
        article2.setDigest("article2");
        article2.setShowConverPic(0);
        entity.addArticle (article2); 
        System.out.println ( "New permanent graphic material, return" + wechatMaterialService.uploadNewsMedia (accessToken, Entity)); 
        // add permanent graphic material, returns tbP52CbH1vuCLlyUAt2rlVvIMceFKJmY663R_urJAYc 
    } 
 
    @Test 
    public void uploadFileForNewsMedia () { 
        String wechatAuthService.getAccessToken accessToken = (); 
        String filePath = "D: \\ Phil the Project \\ \\ wechat the src \\ \\ \\ phil_new_wechat the webapp main \\ \\ \\ 00_01.jpeg Image"; 
        System.out.println (wechatMaterialService.uploadFileForNewsMedia (accessToken, filePath)); 
        //http://mmbiz.qpic.cn/mmbiz_jpg/L8zibbZGicwDw6sv1WWkFK1nCUDY9bJrHiaEbJuTS37XbJR3hEiblAkibzjdTHdHKqXb6Ij53CDfNwztRsOwljibEp8A/0 
    } 
 
    @Test
    public void uploadUrlForNewsMedia() {
        String accessToken = wechatAuthService.getAccessToken();
        String url = "http://www.59xihuan.cn//uploads/allimg/20130728/64511374987081-lp.jpg";
        System.out.println(wechatMaterialService.uploadUrlForNewsMedia(accessToken, url));
        //http://mmbiz.qpic.cn/mmbiz_jpg/L8zibbZGicwDw6sv1WWkFK1nCUDY9bJrHiaQXwtf52SyM4ftxVeuickvh1rDLPMaeE1jDxgpWice9TgJOzYPIRAy8uA/0
    }
 
    @Test
    public void uploadForeverVideoMediaFile() {
        String filePath = "D:/Project/Phil/Wechat/phil_new_wechat/src/main/webapp/video/1539717180390.mp4";
        String accessToken = wechatAuthService.getAccessToken();
        System.out.println ( "New permanent local video clips, return" + wechatMaterialService.uploadForeverMediaFile (accessToken, 
                "title", "Test", filePath)); 
        // add permanent local video clips, return-LTwMdsrKszM7M677NJyic GG16CzQSj3di2OTScAcNA 
    } 
 
    @ the Test 
    public void uploadForeverVideoMediaUrl () { 
        // String URL = "http://vs6.bdstatic.com/browse_static/v3/common/widget/global/player/newPlayer_e2332cd1.swf"; 
        // convert 
        String url = "http: / /xiaozhaozhao.nat300.top/video/1539717180390.mp4 "; 
        String wechatAuthService.getAccessToken accessToken = (); 
        System.out.println (" new persistent network video material returns "+ wechatMaterialService.uploadForeverMediaUrl (accessToken,
                "title", "test", url));
        // add persistent network video material returns GG16CzQSj3di2OTScAcNA6OWJlBHKk8sXSmbgPzxEQo 
    } 
 
    @Test 
    public void getTempMaterial () { 
        String wechatAuthService.getAccessToken accessToken = (); 
        String mediaId = "c985PlW3avLd3w9gUclA75geC9PFFozfX6-Jn6s9kRWwV2gu0KHSaBEDJu0Eugno"; 
        System.out.println (wechatMaterialService.getTempMaterial (accessToken, mediaId)); 
    } 
 
    @Test 
    public void getMaterial () { 
        String wechatAuthService.getAccessToken accessToken = (); 
// String mediaId = "tbP52CbH1vuCLlyUAt2rlUEZKK230SjWIaB9xablAPw"; image // 
        String mediaId = "tbP52CbH1vuCLlyUAt2rlVvIMceFKJmY663R_urJAYc"; // graphic
        System.out.println(wechatMaterialService.getMaterial(accessToken, mediaId));
    }
 
    @Test
    public void deleteMaterial() {
        String mediaId = "tbP52CbH1vuCLlyUAt2rlYaBvU3BHrs8g0-XxdnNILY";
        //tbP52CbH1vuCLlyUAt2rlfnoE7EJNJvPvEv0nFkezsw
        //tbP52CbH1vuCLlyUAt2rlYUYsAvjhY7UmdgQ_8qnip4
        String accessToken = wechatAuthService.getAccessToken();
        System.out.println(wechatMaterialService.deleteMaterial(accessToken, mediaId));
        //ResultState(errcode=0, errmsg=ok)
    }
 
    @Test
    public void geMaterialCount() {
        String accessToken = wechatAuthService.getAccessToken();
        System.out.println(wechatMaterialService.geMaterialCount(accessToken));
        //MaterialCountResult(voiceCount=0, videoCount=5, imageCount=15, newsCount=2)
    }
}

  

Specifically Source: https://github.com/philjing/my_wechat

Guess you like

Origin www.cnblogs.com/phil_jing/p/12077973.html