收藏
回答

开发平台调用提审素材上传接口为什么报错:41005?

{\"errcode\":41005,\"errmsg\":\"media data missing rid: 606eaaba-107d1813-545d7140\"}

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

2 个回答

  • 三个梨
    三个梨
    2021-04-08
    解决了。得用原始的请求。
    
    public static String uploadmedia(String authorizerAccessToken, MultipartFile mediaFile) {
        try {
            URL urlObj = new URL("https://api.weixin.qq.com/wxa/uploadmedia?access_token=" + authorizerAccessToken);
            HttpURLConnection conn = (HttpURLConnection)urlObj.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            String BOUNDARY = "----------" + System.currentTimeMillis();
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
            StringBuilder sb = new StringBuilder();
            sb.append("--");
            sb.append(BOUNDARY);
            sb.append("\r\n");
            sb.append("Content-Disposition:form-data;name=\"media\";filename=\"" + mediaFile.getOriginalFilename() + "\";filelength=\"" + mediaFile.getSize() + "\"\r\n");
            sb.append("Content-Type:application/octet-stream\r\n\r\n");
            byte[] head = sb.toString().getBytes("utf-8");
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(head);
            out.write(mediaFile.getBytes());
            byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
            out.write(foot);
            out.flush();
            out.close();
            StringBuffer buffer = new StringBuffer();
            BufferedReader reader = null;
            String result = null;
    
            try {
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
    
                while((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
    
                if (result == null) {
                    result = buffer.toString();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            } finally {
                reader.close();
            }
    
            return result;
        } catch (IOException var19) {
            logger.error(BizLog.buildLogInfo("uploadmedia: " + ExceptionUtils.getStackTrace(var19)));
            return null;
        }
    }
    


    2021-04-08
    有用
    回复 1
    • 学哲
      学哲
      2022-06-06
      业务服务器 代码 直接读写流吗?有没有优雅的工具?
      2022-06-06
      回复
  • 小李哥
    小李哥
    2021-04-08

    没有正确传递文件内容吧?

    2021-04-08
    有用
    回复 3
    • 三个梨
      三个梨
      2021-04-08
      我就是用httpclient上传的呢? 
      public static String uploadmedia(String authorizerAccessToken, MultipartFile mediaFile){
              try {
                  MultipartEntityBuilder builder = MultipartEntityBuilder.create()
                          .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                          .setCharset(Charset.forName("utf-8"));
                  builder.addBinaryBody("media", mediaFile.getBytes(),ContentType.create(mediaFile.getContentType()),mediaFile.getOriginalFilename());
                  String result = Request.Post("https://api.weixin.qq.com/wxa/uploadmedia?access_token="+authorizerAccessToken)
                          .body(builder.build())
                          .execute().returnContent().asString();
                  logger.info(BizLog.buildLogInfo("uploadmedia,result: "+result));
                  return result;
              } catch (IOException e) {
                  logger.error(BizLog.buildLogInfo("uploadmedia: "+ ExceptionUtils.getStackTrace(e)));
              }
              return null;
          }
      2021-04-08
      回复
    • 学哲
      学哲
      2022-06-06
      这个 Request.Post 是哪个 包下面的呢?
      2022-06-06
      回复
    • 学哲
      学哲
      2022-06-07
      MultipartFile 这个 有没有办法在代码里构建出来?利用byte流?
      2022-06-07
      回复
登录 后发表内容
问题标签