{"record":{"id":"b5211b48bcbba797","repo":"binarywang/WxJava","slug":"error-b5211b","errorCode":null,"errorMessage":"文件[{}]不存在","messagePattern":"文件\\[(.+?)\\]不存在","errorType":"validation","errorClass":"WxRuntimeException","httpStatus":null,"severity":"error","filePath":"weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java","lineNumber":81,"sourceCode":"      conn.setConnectTimeout(60 * 1000);\n      //防止屏蔽程序抓取而返回403错误\n      conn.setRequestProperty(\"User-Agent\", \"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)\");\n      inputStream = conn.getInputStream();\n      return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())\n        , this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),\n        new InputStreamData(inputStream, filename));\n    } finally {\n      IOUtils.closeQuietly(inputStream);\n      if (conn != null) {\n        conn.disconnect();\n      }\n    }\n  }\n\n  @Override\n  public WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException {\n    if(!file.exists()){\n      throw new WxRuntimeException(\"文件[\" + file.getAbsolutePath() + \"]不存在\");\n    }\n    try (InputStream inputStream = Files.newInputStream(file.toPath())) {\n      return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())\n        , this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),\n        new InputStreamData(inputStream, filename));\n    } catch (IOException e) {\n      throw new WxRuntimeException(e);\n    }\n  }\n\n  @Override\n  public WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException{\n    return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())\n      , this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),\n      new InputStreamData(inputStream, filename));\n  }\n\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/binarywang/WxJava/blob/1c43293a3c2c9d7e91304b6d037fb017f680d0c6/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java#L63-L99","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nFile file = new File(uploadedTempPath);\nservice.upload(\"image\", file, file.getName()); // throws if temp file cleaned\n\n// after\nFile file = new File(uploadedTempPath);\nif (file.exists() && file.isFile()) {\n  service.upload(\"image\", file, file.getName());\n} else {\n  throw new BusinessException(\"Media file not found: \" + file.getAbsolutePath());\n}","handlingStrategy":"validation","validationCode":"if (file == null || !file.exists() || !file.isFile()) {\n    throw new IllegalArgumentException(\"File does not exist or is not a regular file: \" + (file != null ? file.getAbsolutePath() : \"null\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["weixin-cp","media","validation","file","precondition"],"backgroundTag":null,"analyzedSha":"1c43293a3c2c9d7e91304b6d037fb017f680d0c6","analyzedAt":"2026-08-14T02:29:11.060Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}