From d5dee4ef1ced9ad457c20107bb7953d538303df0 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 17 Jun 2026 12:07:24 +0100 Subject: [PATCH] Close OutputStream after part created in PartGenerator Closes gh-36945 --- .../converter/multipart/PartGenerator.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/multipart/PartGenerator.java b/spring-web/src/main/java/org/springframework/http/converter/multipart/PartGenerator.java index c4dff4c7ace..8a6bc56b494 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/multipart/PartGenerator.java +++ b/spring-web/src/main/java/org/springframework/http/converter/multipart/PartGenerator.java @@ -337,20 +337,15 @@ final class PartGenerator implements MultipartParser.PartListener { public void onBody(DataBuffer dataBuffer, boolean last) { this.byteCount += dataBuffer.readableByteCount(); if (isMaxDiskUsagePerPartExceeded()) { - try { - this.outputStream.close(); - } - catch (IOException exc) { - // ignored - } - throw new HttpMessageConversionException( - "Part exceeded the disk usage limit of " + - PartGenerator.this.maxDiskUsagePerPart + " bytes"); + closeOutputStream(); + throw new HttpMessageConversionException("Part exceeded " + + "the disk usage limit of " + PartGenerator.this.maxDiskUsagePerPart + " bytes"); } writeBuffer(dataBuffer); if (last) { Part part = DefaultParts.part(this.headers, this.file); PartGenerator.this.addPart(part); + closeOutputStream(); } } @@ -359,6 +354,15 @@ final class PartGenerator implements MultipartParser.PartListener { this.byteCount > PartGenerator.this.maxDiskUsagePerPart); } + private void closeOutputStream() { + try { + this.outputStream.close(); + } + catch (IOException exc) { + // ignored + } + } + private Path createFile(Path directory) { try { Path tempFile = Files.createTempFile(directory, null, ".multipart");