开发者

Extract only file name From FileItem's name

开发者 https://www.devze.com 2023-04-04 13:25 出处:网络
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)

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);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号