Hi,

When uploading a file on Windows, IOException: Failed to delete original file 
is thrown.

This problem happens only on Windows OS when the uploading file is written to 
disk.

The DiskFileItem.write(File) is updated in commons-fileupload 1.4.  It utilizes 
the org.apache.commons.io.FileUtils.moveFile() to copy and delete the .tmp file.

https://github.com/apache/commons-fileupload/blob/11a06cea605eadcba1d6ef125ff1448d6137ed7d/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java#L411

(commons-fileupload 1.5 has the similar call in addition to the remove of 
existing destination file; soI think it will have similar problem).

FileUtils.moveFile(File, File)

https://github.com/apache/commons-io/blob/f22a4227401855ecbfdf8184bbe37275c3aeb5c3/src/main/java/org/apache/commons/io/FileUtils.java#L2392

 if (!srcFile.delete())   can cause IOException on Windows when writing to disk.

To recreate the problem on Tomcat, I patched Tomcat 9.0.72 DiskFileItem to use 
the commons.io FileUtils (also taking care of all its dependent classes) to 
show the exception:

======
27-Feb-2023 08:53:57.416 INFO [main] 
org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: 
[Apache Tomcat/9.0.72]

......
27-Feb-2023 08:54:00.391 INFO [main] org.apache.catalina.startup.Catalina.start 
Server startup in [3073] milliseconds

FileUploadWrite servlet, part.write()

DEBUG DiskFileItem copy from 1.4 source. write()

DEBUG DiskFileItem copy from 1.4 source. to DISK

DEBUG DiskFileItem copy from 1.4 source. FileUtils.moveFile, outputFile 
[C:\TomCat\output-Tomcat-9.72_Patch_1.4Fileupload\build\work\Catalina\localhost\TestFileUpload\upload_b107c4e1_4efe_454a_a9c3_f41186e63e6e_00000004.tmp]
 , file 
[C:\TomCat\output-Tomcat-9.72_Patch_1.4Fileupload\build\work\Catalina\localhost\TestFileUpload\FILEUPLOAD_writingfile.txt]

Exception writing file: java.io.IOException: java.io.IOException: Failed to 
delete original file 
'C:\TomCat\output-Tomcat-9.72_Patch_1.4Fileupload\build\work\Catalina\localhost\TestFileUpload\upload_b107c4e1_4efe_454a_a9c3_f41186e63e6e_00000004.tmp'
 after copy to 
'C:\TomCat\output-Tomcat-9.72_Patch_1.4Fileupload\build\work\Catalina\localhost\TestFileUpload\FILEUPLOAD_writingfile.txt'
======

My servlet app uses javax.servlet.http.Part.write(String) upload file to disk.

This is a regression from commons-fileupload 1.3.

Regards,

Reply via email to