接口如下:接口辅助类如下:handler:impl:
日志信息如下:
把媒体文件上传到微信服务器 请求参数 type[image] filename[vccb4250ddffa0d24f.png]
feign interceptor requestTemplate:
POST /media/upload?access_token=...&type=image HTTP/1.1
Content-Length: 277
Content-Type: multipart/form-data; charset=UTF-8; boundary=180e0d46c02
Binary data
把媒体文件上传到微信服务器 返回结果 {"errorCode":41005,"errorMsg":"media data missing hint: [oOxFFa04598236] rid: 62876163-0fe20ed5-3a41e945"}
public UploadTempMediaResult uploadMedia(String accessToken, String type, File file) { log.info("把媒体文件上传到微信服务器[OkHttpClient] 请求参数 type[{}] filename[{}]", type, file.getName()); try { OkHttpClient client = new OkHttpClient().newBuilder() .build(); // MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("media", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file ) ) .build(); String url = String.format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s", accessToken, type); Request request = new Request.Builder() .url(url) .method("POST", body) .build(); Response response = client.newCall(request).execute(); ResponseBody responseBody = response.body(); if (responseBody == null) { return null; } UploadTempMediaResult uploadTempMediaResult = JSONObject.parseObject(responseBody.string(), UploadTempMediaResult.class); log.info("把媒体文件上传到微信服务器[OkHttpClient] 返回结果 {}", JSONObject.toJSONString(uploadTempMediaResult)); return uploadTempMediaResult; } catch (Exception e) { log.info("uploadMedia e = ", e); } return null; }
我的RestTemplate 上传没问题