Skip to content

Commit 201b9e8

Browse files
committed
Cleans up temporary files
1 parent a1c4cb6 commit 201b9e8

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
4646
* List to track all DiskFileItem instances for proper cleanup
4747
*/
4848
private final List<DiskFileItem> diskFileItems = new ArrayList<>();
49+
50+
/**
51+
* List to track temporary files created for in-memory uploads
52+
*/
53+
private final List<File> temporaryFiles = new ArrayList<>();
4954

5055
@Override
5156
protected void processUpload(HttpServletRequest request, String saveDir) throws IOException {
@@ -111,7 +116,10 @@ protected void processFileField(DiskFileItem item) {
111116
LOG.debug("Creating temporary file representing in-memory uploaded item: {}", normalizeSpace(item.getFieldName()));
112117
try {
113118
File tempFile = File.createTempFile("struts_upload_", "_" + item.getName());
114-
tempFile.deleteOnExit(); // Ensure cleanup on JVM exit
119+
tempFile.deleteOnExit(); // Ensure cleanup on JVM exit as fallback
120+
121+
// Track the temporary file for explicit cleanup
122+
temporaryFiles.add(tempFile);
115123

116124
// Write the in-memory content to the temporary file
117125
try (java.io.FileOutputStream fos = new java.io.FileOutputStream(tempFile)) {
@@ -146,31 +154,63 @@ protected void processFileField(DiskFileItem item) {
146154
}
147155

148156
/**
149-
* Override cleanUp to ensure all DiskFileItem instances are properly cleaned up
157+
* Cleans up disk file items by deleting associated temporary files.
158+
* This method can be overridden by subclasses to customize cleanup behavior.
150159
*/
151-
@Override
152-
public void cleanUp() {
153-
super.cleanUp();
154-
try {
155-
LOG.debug("Clean up all DiskFileItem instances (both form fields and file uploads");
156-
for (DiskFileItem item : diskFileItems) {
157-
try {
158-
if (item.isInMemory()) {
159-
LOG.debug("Cleaning up in-memory item: {}", normalizeSpace(item.getFieldName()));
160-
} else {
161-
LOG.debug("Cleaning up disk item: {} at {}", normalizeSpace(item.getFieldName()), item.getPath());
162-
if (item.getPath() != null && item.getPath().toFile().exists()) {
163-
if (!item.getPath().toFile().delete()) {
164-
LOG.warn("There was a problem attempting to delete temporary file: {}", item.getPath());
165-
}
160+
protected void cleanUpDiskFileItems() {
161+
LOG.debug("Clean up all DiskFileItem instances (both form fields and file uploads");
162+
for (DiskFileItem item : diskFileItems) {
163+
try {
164+
if (item.isInMemory()) {
165+
LOG.debug("Cleaning up in-memory item: {}", normalizeSpace(item.getFieldName()));
166+
} else {
167+
LOG.debug("Cleaning up disk item: {} at {}", normalizeSpace(item.getFieldName()), item.getPath());
168+
if (item.getPath() != null && item.getPath().toFile().exists()) {
169+
if (!item.getPath().toFile().delete()) {
170+
LOG.warn("There was a problem attempting to delete temporary file: {}", item.getPath());
166171
}
167172
}
168-
} catch (Exception e) {
169-
LOG.warn("Error cleaning up DiskFileItem: {}", normalizeSpace(item.getFieldName()), e);
170173
}
174+
} catch (Exception e) {
175+
LOG.warn("Error cleaning up DiskFileItem: {}", normalizeSpace(item.getFieldName()), e);
171176
}
177+
}
178+
}
179+
180+
/**
181+
* Cleans up temporary files created for in-memory uploads.
182+
* This method can be overridden by subclasses to customize cleanup behavior.
183+
*/
184+
protected void cleanUpTemporaryFiles() {
185+
LOG.debug("Cleaning up {} temporary files created for in-memory uploads", temporaryFiles.size());
186+
for (File tempFile : temporaryFiles) {
187+
try {
188+
if (tempFile.exists()) {
189+
LOG.debug("Deleting temporary file: {}", tempFile.getAbsolutePath());
190+
if (!tempFile.delete()) {
191+
LOG.warn("There was a problem attempting to delete temporary file: {}", tempFile.getAbsolutePath());
192+
}
193+
} else {
194+
LOG.debug("Temporary file already deleted: {}", tempFile.getAbsolutePath());
195+
}
196+
} catch (Exception e) {
197+
LOG.warn("Error cleaning up temporary file: {}", tempFile.getAbsolutePath(), e);
198+
}
199+
}
200+
}
201+
202+
/**
203+
* Override cleanUp to ensure all DiskFileItem instances and temporary files are properly cleaned up
204+
*/
205+
@Override
206+
public void cleanUp() {
207+
super.cleanUp();
208+
try {
209+
cleanUpDiskFileItems();
210+
cleanUpTemporaryFiles();
172211
} finally {
173212
diskFileItems.clear();
213+
temporaryFiles.clear();
174214
}
175215
}
176216

0 commit comments

Comments
 (0)