Java 微信获取临时素材对接代码
1.微信文档地址
https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html
2.代码
如果返回的是视频消息素材,则返回的貌似是一个json,那样的话就不适合使用以下代码
我是用来获取微信录音的,获取到文件流之后还需要使用 音频转码工具 AudioUtils.amrToMp3 转为mp3
/*** 获取临时素材* @author hcx* @since 2024-01-03* @return String*/public InputStream getTemporaryMaterial(String mediaId) throws IOException {// 请求微信临时素材接口String token = "access_token";String url = "https://api.weixin.qq.com/cgi-bin/media/get?" + token + "=ACCESS_TOKEN&media_id=" + mediaId;// 请求参数HashMap<String, String> param = new HashMap<>();param.put("media_id",mediaId);// 获取到http连接对象HttpPost httpPost = new HttpPost(url);StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(param));httpPost.setEntity(stringEntity);// 打开链接发送请求 获取到返回的流CloseableHttpClient build = HttpClients.custom().build();CloseableHttpResponse execute = build.execute(httpPost);InputStream inputStream = execute.getEntity().getContent();return inputStream;}