wx-tools 資源上傳相關(guān)API

2023-03-22 17:20 更新

包括如下接口:
- uploadTempMedia - 上傳臨時(shí)資源
- downloadTempMedia - 下載臨時(shí)資源
- uploadMedia - 上傳永久資源
- downloadMedia - 下載永久資源
- downloadNewsMedia - 下載圖文資源
- downloadVideoMedia - 下載視頻資源
- deleteMediaMaterial - 刪除永久資源
- imageDomainChange - 上傳圖片變成騰訊域名下的圖片
- getMaterialCount - 獲取永久資源個(gè)數(shù)
- batchGetMeterial - 批量獲取永久資源

3.3.1 uploadTempMedia 上傳臨時(shí)文件到微信服務(wù)器

  1. //可以上傳file或者InputSteam,拿到MediaID
  2. try {
  3. WxMediaUploadResult result = iService.uploadTempMedia(WxConsts.MEDIA_IMAGE,new File("E://test.jpg"));
  4. System.out.println(result.getMedia_id());
  5. } catch (WxErrorException e) {
  6. e.printStackTrace();
  7. }

3.3.2 downloadTempMedia 下載臨時(shí)文件,存在E://temp文件夾

  1. try {
  2. File file = iService.downloadTempMedia("media_id",new File("E://temp"));
  3. } catch (WxErrorException e) {
  4. e.printStackTrace();
  5. }

3.3.3 uploadMedia 上傳永久文件到微信服務(wù)器。可以傳File或者輸入流

  1. try {
  2. //這里注意,如果是上傳非視頻格式的素材,第三個(gè)參數(shù)(WxVideoIntroduction)為null即可
  3. WxMediaUploadResult result1 = iService.uploadMedia(WxConsts.MEDIA_VOICE, new File("E://test.m4a"), null);
  4. //如果是上傳視頻Video,可以添加描述
  5. WxVideoIntroduction intro = new WxVideoIntroduction();
  6. intro.setTitle("視頻1");
  7. intro.setIntroduction("描述1");
  8. WxMediaUploadResult result2 = iService.uploadMedia(WxConsts.MEDIA_VIDEO, new File("E://test.mp4"), intro);
  9. } catch (WxErrorException e) {
  10. e.printStackTrace();
  11. }

3.3.4 downloadMedia 下載永久文件,與臨時(shí)文件一樣用法(注意:圖文和視頻需要使用另外的方法)

  1. try {
  2. File file = iService.downloadMedia("media_id",new File("E://temp"));
  3. } catch (WxErrorException e) {
  4. e.printStackTrace();
  5. }

3.3.5 downloadNewsMedia 下載圖文素材

  1. try {
  2. //圖文結(jié)果
  3. WxNewsMediaResult result = iService.downloadNewsMedia("media_id");
  4. System.out.println(result.toString());
  5. } catch (WxErrorException e) {
  6. e.printStackTrace();
  7. }

3.3.6 downloadVideoMedia 下載視頻素材

  1. try {
  2. //視頻結(jié)果,取出URL即可下載
  3. WxVideoMediaResult result = iService.downloadVideoMedia("media_id", new File("E://temp"));
  4. System.out.println(result.toString());
  5. } catch (WxErrorException e) {
  6. e.printStackTrace();
  7. }

3.3.7 deleteMediaMaterial 刪除素材資源

  1. try {
  2. WxError result = iService.deleteMediaMaterial("media_id");
  3. System.out.println(result.getErrcode());
  4. } catch (WxErrorException e) {
  5. e.printStackTrace();
  6. }

3.3.8 imageDomainChange 上傳圖片變成騰訊域名下的圖片

  1. try {
  2. WxMediaUploadResult result = iService.imageDomainChange(new File("E://test.jpg"));
  3. System.out.println(result.getUrl());
  4. } catch (WxErrorException e) {
  5. e.printStackTrace();
  6. }

3.3.9 getMaterialCount 獲取永久素材數(shù)量接口

  1. try {
  2. WxMaterialCountResult result = iService.getMaterialCount();
  3. System.out.println(result.getImage_count());
  4. System.out.println(result.getNews_count());
  5. } catch (WxErrorException e) {
  6. e.printStackTrace();
  7. }

3.3.10 batchGetMeterial 批量獲取永久素材資源信息

  1. try {
  2. WxBatchGetMaterialResult result = iService.batchGetMeterial(WxConsts.MEDIA_IMAGE, 0, 5);
  3. } catch (WxErrorException e) {
  4. e.printStackTrace();
  5. }

  • type 永久素材類型
  • offset 頁(yè)碼
  • count 數(shù)量
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)