{"record":{"id":"28bf53baa1b5c623","repo":"OpenFeign/feign","slug":"getting-multipart-file-s-content-bytes-error","errorCode":null,"errorMessage":"Getting multipart file's content bytes error","messagePattern":"Getting multipart file's content bytes error","errorType":"exception","errorClass":"EncodeException","httpStatus":null,"severity":"error","filePath":"form-spring/src/main/java/feign/form/spring/SpringSingleMultipartFileWriter.java","lineNumber":46,"sourceCode":" * @author Artem Labazin\n */\npublic class SpringSingleMultipartFileWriter extends AbstractWriter {\n\n  @Override\n  public boolean isApplicable(Object value) {\n    return value instanceof MultipartFile;\n  }\n\n  @Override\n  protected void write(Output output, String key, Object value) throws EncodeException {\n    val file = (MultipartFile) value;\n    writeFileMetadata(output, key, file.getOriginalFilename(), file.getContentType());\n\n    byte[] bytes;\n    try {\n      bytes = file.getBytes();\n    } catch (IOException ex) {\n      throw new EncodeException(\"Getting multipart file's content bytes error\", ex);\n    }\n    output.write(bytes);\n  }\n}\n","sourceCodeStart":28,"sourceCodeEnd":51,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/form-spring/src/main/java/feign/form/spring/SpringSingleMultipartFileWriter.java#L28-L51","documentation":"SpringSingleMultipartFileWriter.write() wraps any IOException from MultipartFile.getBytes() in a Feign EncodeException. It means the library could not read the multipart file's content bytes while encoding the request body.","triggerScenarios":"Encoding a Spring MultipartFile via feign-form when getBytes() fails, e.g. the underlying temp file was deleted, the storage backend is unavailable, or disk I/O errors occur.","commonSituations":"StandardMultipartFile temp files cleaned up before the request is sent; files stored in a remote/temp storage that has been purged; oversized uploads failing during read.","solutions":["Verify the MultipartFile is still valid (not a temp file already deleted) before sending","Read bytes yourself earlier and check availability, or stream via getInputStream() with a custom writer","Check disk space and temp directory permissions on the uploading side","Inspect the wrapped IOException cause for the storage-level root cause"],"exampleFix":"// before\nfeignBuilder.target(UploadApi.class).upload(file);\n// after\nif (!file.isEmpty()) {\n  feignBuilder.target(UploadApi.class).upload(file);\n} else {\n  throw new IllegalStateException(\"MultipartFile is empty or already consumed\");\n}","handlingStrategy":"try-catch","validationCode":"if (file == null || file.isEmpty()) {\n  throw new IllegalStateException(\"MultipartFile is empty or missing\");\n}","typeGuard":"boolean sendable(MultipartFile f) { return f != null && !f.isEmpty(); }","tryCatchPattern":"try {\n  api.upload(file);\n} catch (EncodeException e) {\n  logger.error(\"Could not read multipart bytes: {}\", e.getCause());\n}","preventionTips":["Send files while the request/temp storage is still valid","Check file.isEmpty() before upload","Monitor temp directory cleanup policies and disk space"],"tags":["multipart","encoding","io","file-upload"],"backgroundTag":"file-read-failed","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}