简体   繁体   English

仅从FileItem的名称中提取文件名

[英]Extract only file name From FileItem's name

In apache commons file uploading the uploaded files are available as FileItem objects. 在apache commons文件中,上传上传的文件可用作FileItem对象。
If I get the name of such file item by using fileitem.getName() it returns the full path (ex: C:\\Test\\test.txt ). 如果我通过使用fileitem.getName()获取此类文件项的名称,则返回完整路径(例如: 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() 我真正需要的是使用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 ) 但临时文件的名称和扩展名应与上传的文件相同,而不是随机名称(如temp.tmp

Try what the docs say 试试文档说的内容

Why does FileItem.getName() return the whole path, and not just the file name? 为什么FileItem.getName()返回整个路径,而不仅仅是文件名?

Internet Explorer provides the entire path to the uploaded file and not just the base file name. Internet Explorer提供上载文件的完整路径,而不仅仅是基本文件名。 Since FileUpload provides exactly what was supplied by the client (browser), you may want to remove this path information in your application. 由于FileUpload完全提供客户端(浏览器)提供的内容,因此您可能希望在应用程序中删除此路径信息。 You can do that using the following method from Commons IO (which you already have, since it is used by FileUpload). 您可以使用Commons IO中的以下方法(您已经拥有它,因为它由FileUpload使用)。

String fileName = item.getName();
 if (fileName != null) {
     fileName = FilenameUtils.getName(fileName);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM