From 327e761536eb23c9db90e93c18a549b6bccca2f8 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 12 Apr 2021 16:26:53 +0200 Subject: [PATCH] Recreate file storage directory when removed in DefaultPartHttpMessageReader Some operating systems delete temp files not just when booting up, but also during operation. This commit makes sure that the DefaultPartHttpMessageReader recreates the directory used to store files in, if it's not there. Closes gh-26790 --- .../http/codec/multipart/PartGenerator.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java index 39aa9149fa..3e684a47fb 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -53,7 +54,7 @@ import org.springframework.util.FastByteArrayOutputStream; /** * Subscribes to a token stream (i.e. the result of - * {@link MultipartParser#parse(Flux, byte[], int)}, and produces a flux of {@link Part} objects. + * {@link MultipartParser#parse(Flux, byte[], int, Charset)}, and produces a flux of {@link Part} objects. * * @author Arjen Poutsma * @since 5.3 @@ -577,6 +578,9 @@ final class PartGenerator extends BaseSubscriber { private WritingFileState createFileState(Path directory) { try { + if (!Files.exists(directory)) { + Files.createDirectory(directory); + } Path tempFile = Files.createTempFile(directory, null, ".multipart"); if (logger.isTraceEnabled()) { logger.trace("Storing multipart data in file " + tempFile); -- GitLab