首先通过提审素材上传接口上传了图片,返回了 mediaid
接口地址:https://api.weixin.qq.com/wxa/uploadmedia
返回参数:
{
"type": "image",
"mediaid": "4GifapERF3pD6QzqGHFPxcVKo0nHwX-rg-JtJQgTwCAz-4-ml1P6XPFORA9Sq05yBp3W09iXoGzvFO-_Tr5vIg",
"errcode": 0,
"errmsg": "ok"
}
然后通过获取永久素材接口,通过该mediaid获取图片,上传到oss中或者生成本地图片,生成的图片无法打开
接口地址:https://api.weixin.qq.com/cgi-bin/material/get_material
获取后上传到oss中,返回的地址是:
http://ztkj-wechat.oss-cn-shenzhen.aliyuncs.com/20220629143925593.jpg
生成的图片打开提示如下截图:
相关代码如下:
上传代码:
//请求头设置
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentLength(qo.getMedia().getSize());
//设置请求体,注意是LinkedMultiValueMap
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("media", qo.getMedia().getResource());
String url = CallWeChatApiServiceHelp.getUrlByAuthorizerAccessToken(WeChatEnums.WechatApiUrl.uploadmedia, qo);
UploadMediaResDto uploadMediaResDto = new RestTemplate().postForObject(url, new HttpEntity<>(form, headers), UploadMediaResDto.class);
获取素材接口代码如下:
String url = CallWeChatApiServiceHelp.getUrlByAuthorizerAccessToken(WeChatEnums.WechatApiUrl.get_material, qo);
GetMaterialReqDto getMaterialReqDto = new GetMaterialReqDto().setMedia_id(qo.getMediaId());
ResponseEntity<byte[]> rsp = new RestTemplate().exchange(url, HttpMethod.POST, new HttpEntity<>(getMaterialReqDto), byte[].class);
Assert.notNull(rsp, "调用获取永久素材接口返回为null");
//上传图片
String newFileName = String.format("%s.jpg", CommonUtil.createCurrentGeneratorFolderNameByTimeStamp());
String remotePath = String.format("%s/%s", CommonConstant.WXCHAT_MINIO_UPLOAD_PREFIX, newFileName);
String uploadFile = MinIoUtil.uploadFile(this.minIoProperties, newFileName, new ByteArrayInputStream(rsp.getBody()), remotePath);
寻求帮助,谢谢