binarywang/WxJava · error · WxRuntimeException
文件[{}]不存在
Error message
文件[{}]不存在 What it means
Thrown as WxRuntimeException (unchecked) in the upload(mediaType, file, filename) overload of WxCpMediaServiceImpl when file.exists() returns false. The method wraps the file into an InputStreamData for the multipart upload executor; if the file path does not point to a real file on disk, it fails before attempting any network operation.
Source
Thrown at weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java:81
conn.setConnectTimeout(60 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
inputStream = conn.getInputStream();
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
new InputStreamData(inputStream, filename));
} finally {
IOUtils.closeQuietly(inputStream);
if (conn != null) {
conn.disconnect();
}
}
}
@Override
public WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException {
if(!file.exists()){
throw new WxRuntimeException("文件[" + file.getAbsolutePath() + "]不存在");
}
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
new InputStreamData(inputStream, filename));
} catch (IOException e) {
throw new WxRuntimeException(e);
}
}
@Override
public WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException{
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
new InputStreamData(inputStream, filename));
}
View on GitHub (pinned to 1c43293a3c)
Solutions
- Verify file.exists() and file.isFile() before calling upload.
- Use absolute paths (file.getAbsolutePath()) and log them to diagnose path mismatches.
- Ensure the file lifecycle (creation, upload, cleanup) is managed so the file still exists when upload is called.
Example fix
// before
File file = new File(uploadedTempPath);
service.upload("image", file, file.getName()); // throws if temp file cleaned
// after
File file = new File(uploadedTempPath);
if (file.exists() && file.isFile()) {
service.upload("image", file, file.getName());
} else {
throw new BusinessException("Media file not found: " + file.getAbsolutePath());
} Defensive patterns
Strategy: validation
Validate before calling
if (file == null || !file.exists() || !file.isFile()) {
throw new IllegalArgumentException("File does not exist or is not a regular file: " + (file != null ? file.getAbsolutePath() : "null"));
} Prevention
- Verify file.exists() and file.isFile() before calling upload.
- Use absolute paths to avoid working-directory ambiguity.
- Manage file lifecycle carefully in request-scoped code to prevent premature cleanup of temp files.
When it happens
Trigger: Calling cpService.getMediaService().upload(mediaType, file, filename) where the File object points to a path that does not exist on the filesystem. Happens with incorrect absolute/relative paths, files deleted between check and upload, or temp files already cleaned up.
Common situations: Developer constructs File from a user-uploaded path that has already been garbage-collected by the servlet container's temp directory. Or the path is relative and the working directory differs at runtime.
Related errors
- 更新「联系我」方式需要指定configId
- userid 不能为空
- fieldList 不能为空
- userid_list和department_id_list至少需要填其中一个
- uri参数中不允许有access_token: {uri}
AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14).
Data as JSON: /api/errors/b5211b48bcbba797.
Report an issue: GitHub.