@Test public void testUpload() { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("media/image.png"); String mediaId = Medias.defaultMedias().upload(MediaType.image, inputStream, "image.png"); Assert.assertNotNull(mediaId); }
public String upload(MediaType type, InputStream inputStream, String fileName) { if (type == MediaType.mpnews) { throw new com.riversoft.weixin.common.exception.WxRuntimeException(999, "unsupported media type: " + type.name()); } String url = WxEndpoint.get("url.media.upload"); String response = wxClient.post(String.format(url, type.name()), inputStream, fileName); Map<String, Object> result = JsonMapper.defaultMapper().json2Map(response); if (result.containsKey("media_id")) { return result.get("media_id").toString(); } else { logger.warn("media upload failed: {}", response); throw new WxRuntimeException(998, response); } }
Medias { public String upload(MediaType type, InputStream inputStream, String fileName) { if (type == MediaType.mpnews) { throw new com.riversoft.weixin.common.exception.WxRuntimeException(999, "unsupported media type: " + type.name()); } String url = WxEndpoint.get("url.media.upload"); String response = wxClient.post(String.format(url, type.name()), inputStream, fileName); Map<String, Object> result = JsonMapper.defaultMapper().json2Map(response); if (result.containsKey("media_id")) { return result.get("media_id").toString(); } else { logger.warn("media upload failed: {}", response); throw new WxRuntimeException(998, response); } } }
Medias { public String upload(MediaType type, InputStream inputStream, String fileName) { if (type == MediaType.mpnews) { throw new com.riversoft.weixin.common.exception.WxRuntimeException(999, "unsupported media type: " + type.name()); } String url = WxEndpoint.get("url.media.upload"); String response = wxClient.post(String.format(url, type.name()), inputStream, fileName); Map<String, Object> result = JsonMapper.defaultMapper().json2Map(response); if (result.containsKey("media_id")) { return result.get("media_id").toString(); } else { logger.warn("media upload failed: {}", response); throw new WxRuntimeException(998, response); } }  }
Medias { public String upload(MediaType type, InputStream inputStream, String fileName) { if (type == MediaType.mpnews) { throw new com.riversoft.weixin.common.exception.WxRuntimeException(999, "unsupported media type: " + type.name()); } String url = WxEndpoint.get("url.media.upload"); String response = wxClient.post(String.format(url, type.name()), inputStream, fileName); Map<String, Object> result = JsonMapper.defaultMapper().json2Map(response); if (result.containsKey("media_id")) { return result.get("media_id").toString(); } else { logger.warn("media upload failed: {}", response); throw new WxRuntimeException(998, response); } }  static Medias defaultMedias(); static Medias with(CorpSetting corpSetting); void setWxClient(WxClient wxClient); String upload(MediaType type, InputStream inputStream, String fileName); File download(String mediaId); }
Medias { public String upload(MediaType type, InputStream inputStream, String fileName) { if (type == MediaType.mpnews) { throw new com.riversoft.weixin.common.exception.WxRuntimeException(999, "unsupported media type: " + type.name()); } String url = WxEndpoint.get("url.media.upload"); String response = wxClient.post(String.format(url, type.name()), inputStream, fileName); Map<String, Object> result = JsonMapper.defaultMapper().json2Map(response); if (result.containsKey("media_id")) { return result.get("media_id").toString(); } else { logger.warn("media upload failed: {}", response); throw new WxRuntimeException(998, response); } }  static Medias defaultMedias(); static Medias with(CorpSetting corpSetting); void setWxClient(WxClient wxClient); String upload(MediaType type, InputStream inputStream, String fileName); File download(String mediaId);  }