In apache commons file uploading the uploaded files are available as FileItem objects.
If I get the name of such file item by using fileitem.getName()
it returns the full path (ex: C:\Test\test.txt
).
Is there any way to get only the file name.
What I actually need is to save the uploaded file as temp file using File.createTempFile()
but the name and extension of the temp file should be the same as the uploaded file rather than a random name(like temp.tmp
)
Try what the docs say
Why does FileItem.getName() return the whole path, and not just the file name?
Internet Explorer provides the entire path to the uploaded file and not just the base file name. Since FileUpload provides exactly what was supplied by the client (browser), you may want to remove this path information in your application. You can do that using the following method from Commons IO (which you already have, since it is used by FileUpload).
String fileName = item.getName();
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With