收藏
回答

把媒体文件上传到微信服务器,提示:media data missing?

接口如下:接口辅助类如下: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"}



回答关注问题邀请回答
收藏

2 个回答

  • L
    L
    2022-05-21
        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;
        }
    


    2022-05-21
    有用
    回复 1
    • L
      L
      2022-05-21
      2022-05-21
      回复
  • Mr.Zhao
    Mr.Zhao
    2022-05-20

    我的RestTemplate 上传没问题

    2022-05-20
    有用
    回复 3
    • L
      L
      2022-05-20
      我这里都已经上线一跑了几个月了,也是没问题,最近突然不行了
      2022-05-20
      回复
    • Mr.Zhao
      Mr.Zhao
      2022-05-20回复L
      那你用postman 也是会报错?
      2022-05-20
      回复
    • L
      L
      2022-05-21回复Mr.Zhao
      换了okhttp,好了
      2022-05-21
      回复
登录 后发表内容