binarywang/WxJava · error · WxErrorException

getmediadata err ret {}

Error message

getmediadata err ret {}

What it means

Thrown when Finance.GetMediaData() returns a non-zero code during chunked media-file download from the chat archive. This call fetches binary media (images, voice, video, files) identified by sdkfileid, optionally through a proxy. A non-zero return signals the native SDK could not retrieve the media chunk.

Source

Thrown at weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java:269

    });
  }

  @Override
  public void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull Consumer<byte[]> action) throws WxErrorException {
    /**
     * 1、媒体文件每次拉取的最大size为512k,因此超过512k的文件需要分片拉取。
     * 2、若该文件未拉取完整,sdk的IsMediaDataFinish接口会返回0,同时通过GetOutIndexBuf接口返回下次拉取需要传入GetMediaData的indexbuf。
     * 3、indexbuf一般格式如右侧所示,”Range:bytes=524288-1048575“:表示这次拉取的是从524288到1048575的分片。单个文件首次拉取填写的indexbuf为空字符串,拉取后续分片时直接填入上次返回的indexbuf即可。
     */
    String indexbuf = "";
    int ret, data_len = 0;
    log.debug("正在分片拉取媒体文件 sdkFileId为{}", sdkfileid);
    while (true) {
      long mediaData = Finance.NewMediaData();
      ret = Finance.GetMediaData(sdk, indexbuf, sdkfileid, proxy, passwd, timeout, mediaData);
      if (ret != 0) {
        Finance.FreeMediaData(mediaData);
        throw new WxErrorException("getmediadata err ret " + ret);
      }

      data_len += Finance.GetDataLen(mediaData);
      log.debug("正在分片拉取媒体文件 len:{}, data_len:{}, is_finish:{} \n", Finance.GetIndexLen(mediaData), data_len, Finance.IsMediaDataFinish(mediaData));

      try {
        // 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。
        action.accept(Finance.GetData(mediaData));
      } catch (Exception e) {
        log.error("处理媒体文件分片失败,sdkfileid={}", sdkfileid, e);
      }

      if (Finance.IsMediaDataFinish(mediaData) == 1) {
        // 已经拉取完成最后一个分片
        Finance.FreeMediaData(mediaData);
        break;
      } else {
        // 获取下次拉取需要使用的indexbuf

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Download media promptly after retrieving chat records — WeChat retains media files for a limited window only
  2. If using a proxy, verify proxy host, port, and credentials (passwd parameter) are correct and reachable
  3. Increase the timeout parameter to accommodate large files (each chunk is up to 512 KB, multi-GB files need many chunks)
  4. Ensure the SDK handle is freshly initialized via getOrInitThreadLocalSdk() before the media download loop
  5. Check the native return code: 10001/10002 indicate SDK or token problems, network-range codes indicate connectivity issues

Example fix

// before
ret = Finance.GetMediaData(sdk, indexbuf, sdkfileid, proxy, passwd, timeout, mediaData);

// after — validate sdk handle and use generous timeout for large files
if (sdk == 0) {
  throw new WxErrorException("SDK 未初始化,无法下载媒体文件");
}
long effectiveTimeout = Math.max(timeout, 30000); // at least 30s per chunk
ret = Finance.GetMediaData(sdk, indexbuf, sdkfileid, proxy, passwd, effectiveTimeout, mediaData);
if (ret != 0) {
  log.warn("媒体下载失败 ret={}, sdkfileid={} (可能已过期)", ret, sdkfileid);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate before media download
if (sdk == 0) {
  throw new IllegalStateException("SDK 未初始化");
}
if (StringUtils.isBlank(sdkfileid)) {
  throw new IllegalArgumentException("sdkfileid 不能为空");
}
// Download media within a reasonable time window after fetching chat records

Try / catch

try {
  msgAuditService.getMediaFile(sdk, sdkfileid, proxy, passwd, 60000, targetPath);
} catch (WxErrorException e) {
  log.warn("媒体文件下载失败 sdkfileid={}: {}", sdkfileid, e.getMessage());
  // Media may have expired; skip and continue processing other records
  if (e.getMessage().contains("err ret")) {
    log.warn("媒体可能已过期或网络不可达,跳过此文件");
  }
}

Prevention

When it happens

Trigger: Calling getMediaFile() with an sdkfileid that is expired (media is only downloadable for a limited time after the message), an uninitialized/stale SDK handle, an unreachable or misconfigured proxy, a network timeout shorter than the actual download time, or an sdkfileid from a different access_token session.

Common situations: Media files expire — attempting to download long after the chat message was archived; proxy credentials are wrong or the proxy host is unreachable; the timeout value is too low for large multi-chunk files; the SDK handle was closed or the process restarted losing the native context; sdkfileid was extracted from a re-fetched seq that no longer maps to valid media.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/09a6a60f297349cf. Report an issue: GitHub.