Skip to content

Commit a64d371

Browse files
committed
Merge branch '5.3.x'
2 parents 9b739a2 + aa8b06b commit a64d371

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import java.nio.channels.Channel;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2325

2426
import org.springframework.core.io.buffer.DataBuffer;
2527
import org.springframework.http.HttpHeaders;
@@ -110,6 +112,14 @@ public static void closeChannel(Channel channel) {
110112
}
111113
}
112114

115+
public static void deleteFile(Path file) {
116+
try {
117+
Files.delete(file);
118+
}
119+
catch (IOException ignore) {
120+
}
121+
}
122+
113123
public static boolean isFormField(HttpHeaders headers) {
114124
MediaType contentType = headers.getContentType();
115125
return (contentType == null || MediaType.TEXT_PLAIN.equalsTypeAndSubtype(contentType))

spring-web/src/main/java/org/springframework/http/codec/multipart/PartGenerator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ private void fileCreated(WritingFileState newState) {
555555
}
556556
else {
557557
MultipartUtils.closeChannel(newState.channel);
558+
MultipartUtils.deleteFile(newState.file);
558559
this.content.forEach(DataBufferUtils::release);
559560
}
560561
}
@@ -586,6 +587,8 @@ private final class IdleFileState implements State {
586587

587588
private volatile boolean closeOnDispose = true;
588589

590+
private volatile boolean deleteOnDispose = true;
591+
589592

590593
public IdleFileState(WritingFileState state) {
591594
this.headers = state.headers;
@@ -600,16 +603,20 @@ public void body(DataBuffer dataBuffer) {
600603
if (PartGenerator.this.maxDiskUsagePerPart == -1 || count <= PartGenerator.this.maxDiskUsagePerPart) {
601604

602605
this.closeOnDispose = false;
606+
this.deleteOnDispose = false;
603607
WritingFileState newState = new WritingFileState(this);
604608
if (changeState(this, newState)) {
605609
newState.writeBuffer(dataBuffer);
606610
}
607611
else {
608612
MultipartUtils.closeChannel(this.channel);
613+
MultipartUtils.deleteFile(this.file);
609614
DataBufferUtils.release(dataBuffer);
610615
}
611616
}
612617
else {
618+
MultipartUtils.closeChannel(this.channel);
619+
MultipartUtils.deleteFile(this.file);
613620
DataBufferUtils.release(dataBuffer);
614621
emitError(new DataBufferLimitException(
615622
"Part exceeded the disk usage limit of " + PartGenerator.this.maxDiskUsagePerPart +
@@ -620,6 +627,7 @@ public void body(DataBuffer dataBuffer) {
620627
@Override
621628
public void onComplete() {
622629
MultipartUtils.closeChannel(this.channel);
630+
this.deleteOnDispose = false;
623631
emitPart(DefaultParts.part(this.headers, this.file, PartGenerator.this.blockingOperationScheduler));
624632
}
625633

@@ -628,6 +636,9 @@ public void dispose() {
628636
if (this.closeOnDispose) {
629637
MultipartUtils.closeChannel(this.channel);
630638
}
639+
if (this.deleteOnDispose) {
640+
MultipartUtils.deleteFile(this.file);
641+
}
631642
}
632643

633644

@@ -651,6 +662,8 @@ private final class WritingFileState implements State {
651662

652663
private volatile boolean completed;
653664

665+
private volatile boolean disposed;
666+
654667

655668
public WritingFileState(CreateFileState state, Path file, WritableByteChannel channel) {
656669
this.headers = state.headers;
@@ -701,11 +714,15 @@ private void writeComplete() {
701714
if (this.completed) {
702715
newState.onComplete();
703716
}
717+
else if (this.disposed) {
718+
newState.dispose();
719+
}
704720
else if (changeState(this, newState)) {
705721
requestToken();
706722
}
707723
else {
708724
MultipartUtils.closeChannel(this.channel);
725+
MultipartUtils.deleteFile(this.file);
709726
}
710727
}
711728

@@ -719,13 +736,21 @@ private Mono<Void> writeInternal(DataBuffer dataBuffer) {
719736
return Mono.empty();
720737
}
721738
catch (IOException ex) {
739+
MultipartUtils.closeChannel(this.channel);
740+
MultipartUtils.deleteFile(this.file);
722741
return Mono.error(ex);
723742
}
724743
finally {
725744
DataBufferUtils.release(dataBuffer);
726745
}
727746
}
728747

748+
@Override
749+
public void dispose() {
750+
this.disposed = true;
751+
}
752+
753+
729754
@Override
730755
public String toString() {
731756
return "WRITE-FILE";

0 commit comments

Comments
 (0)