Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract only file name From FileItem's name

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)

like image 972
Dinushan Avatar asked Sep 14 '11 06:09

Dinushan


1 Answers

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);
 }
like image 196
JoseK Avatar answered Nov 15 '22 10:11

JoseK