OpenFeign/feign · error · EncodeException

Output closing error

Error message

Output closing error

What it means

MultipartFormContentProcessor writes all parts into an internal ByteArrayOutputStream; if closing/flushing that stream raises IOException while processing the multipart template, the IOException is wrapped in an EncodeException with message 'Output closing error'.

Solutions

  1. Inspect the wrapped cause (EncodeException.getCause()) to find the actual failing writer/file
  2. Verify all files being encoded are readable and not deleted/concurrently modified
  3. Catch EncodeException around Feign encoding calls and log the cause
  4. Update feign-form if an older version had stream-handling bugs

Example fix

// before
try {
  feignClient.upload(file);
} catch (EncodeException e) {
  log.error("encode failed");
}
// after
try {
  feignClient.upload(file);
} catch (EncodeException e) {
  log.error("encode failed: " + e.getCause(), e); // see real IOException
}
Defensive patterns

Strategy: try-catch

Validate before calling

for (File f : files) {
  if (!f.canRead()) throw new IllegalStateException("unreadable file: " + f);
}

Try / catch

try {
  client.upload(file);
} catch (EncodeException e) {
  log.error("multipart encoding failed", e.getCause());
  throw new UncheckedIOException((IOException) e.getCause());
}

Prevention

When it happens

Trigger: process() called (e.g. while generating a boundary and writing parts) and an IOException occurs on the underlying output stream — e.g. a part writer failing mid-stream, memory/IO pressure on the ByteArrayOutputStream path.

Common situations: Encoding a multipart request where one of the registered writers throws an IOException (often surfacing here rather than at the writer), or an interrupted write of a large file body.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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

Appendix: source

Thrown at form/src/main/java/feign/form/MultipartFormContentProcessor.java:111

      val contentTypeHeaderValue =
          new StringBuilder()
              .append(getSupportedContentType().getHeader())
              .append("; charset=")
              .append(charset.name())
              .append("; boundary=")
              .append(boundary)
              .toString();

      template.header(CONTENT_TYPE_HEADER, Collections.<String>emptyList()); // reset header
      template.header(CONTENT_TYPE_HEADER, contentTypeHeaderValue);

      // Feign's clients try to determine binary/string content by charset presence
      // so, I set it to null (in spite of availability charset) for backward
      // compatibility.
      val bytes = output.toByteArray();
      template.body(bytes, null);
    } catch (IOException ex) {
      throw new EncodeException("Output closing error", ex);
    }
  }

  @Override
  public ContentType getSupportedContentType() {
    return MULTIPART;
  }

  /**
   * Adds {@link Writer} instance in runtime.
   *
   * @param writer additional writer.
   */
  public final void addWriter(Writer writer) {
    writers.add(writer);
  }

  /**

View on GitHub (pinned to e2a1e27560)