@@ -46,6 +46,11 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
46
46
* List to track all DiskFileItem instances for proper cleanup
47
47
*/
48
48
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 <>();
49
54
50
55
@ Override
51
56
protected void processUpload (HttpServletRequest request , String saveDir ) throws IOException {
@@ -111,7 +116,10 @@ protected void processFileField(DiskFileItem item) {
111
116
LOG .debug ("Creating temporary file representing in-memory uploaded item: {}" , normalizeSpace (item .getFieldName ()));
112
117
try {
113
118
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 );
115
123
116
124
// Write the in-memory content to the temporary file
117
125
try (java .io .FileOutputStream fos = new java .io .FileOutputStream (tempFile )) {
@@ -146,31 +154,63 @@ protected void processFileField(DiskFileItem item) {
146
154
}
147
155
148
156
/**
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.
150
159
*/
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 ());
166
171
}
167
172
}
168
- } catch (Exception e ) {
169
- LOG .warn ("Error cleaning up DiskFileItem: {}" , normalizeSpace (item .getFieldName ()), e );
170
173
}
174
+ } catch (Exception e ) {
175
+ LOG .warn ("Error cleaning up DiskFileItem: {}" , normalizeSpace (item .getFieldName ()), e );
171
176
}
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 ();
172
211
} finally {
173
212
diskFileItems .clear ();
213
+ temporaryFiles .clear ();
174
214
}
175
215
}
176
216
0 commit comments