OpenFeign/feign · error · EncodeException

Getting multipart file's content bytes error

Error message

Getting multipart file's content bytes error

What it means

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.

Solutions

  1. Verify the MultipartFile is still valid (not a temp file already deleted) before sending
  2. Read bytes yourself earlier and check availability, or stream via getInputStream() with a custom writer
  3. Check disk space and temp directory permissions on the uploading side
  4. Inspect the wrapped IOException cause for the storage-level root cause

Example fix

// before
feignBuilder.target(UploadApi.class).upload(file);
// after
if (!file.isEmpty()) {
  feignBuilder.target(UploadApi.class).upload(file);
} else {
  throw new IllegalStateException("MultipartFile is empty or already consumed");
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (file == null || file.isEmpty()) {
  throw new IllegalStateException("MultipartFile is empty or missing");
}

Type guard

boolean sendable(MultipartFile f) { return f != null && !f.isEmpty(); }

Try / catch

try {
  api.upload(file);
} catch (EncodeException e) {
  logger.error("Could not read multipart bytes: {}", e.getCause());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10). Data as JSON: /api/errors/28bf53baa1b5c623. Report an issue: GitHub.

Appendix: source

Thrown at form-spring/src/main/java/feign/form/spring/SpringSingleMultipartFileWriter.java:46

 * @author Artem Labazin
 */
public class SpringSingleMultipartFileWriter extends AbstractWriter {

  @Override
  public boolean isApplicable(Object value) {
    return value instanceof MultipartFile;
  }

  @Override
  protected void write(Output output, String key, Object value) throws EncodeException {
    val file = (MultipartFile) value;
    writeFileMetadata(output, key, file.getOriginalFilename(), file.getContentType());

    byte[] bytes;
    try {
      bytes = file.getBytes();
    } catch (IOException ex) {
      throw new EncodeException("Getting multipart file's content bytes error", ex);
    }
    output.write(bytes);
  }
}

View on GitHub (pinned to e2a1e27560)