event.getFile().getFileName() 使用 PrimeFaces 3.5 在 JSF2.0 中返回带有完整路径的文件名 [英] event.getFile().getFileName() is returning filename with complete path in JSF2.0 with PrimeFaces 3.5
问题描述
我使用 PrimeFaces v3.5 使用 Firefox 浏览器将文件上传到我的 Windows 机器中.event.getFile().getFileName()
正在返回带有完整路径的文件名,这进一步导致了问题.PrimeFaces 在内部使用 Apache 公共资源.我检查了 javadoc 也是,但不再帮助我了.
I am using PrimeFaces v3.5 to upload the files in my windows machine using Firefox browser. event.getFile().getFileName()
is returning filename with complete path which is causing problems further. Internally PrimeFaces is using Apache commons. I checked the javadoc also but not helping me anymore.
为了解决这个问题,我对程序进行了如下修改-
To overcome with this issue, I modified the program a little bit like following manner-
String fileName = event.getFile().getFileName();
fileName = fileName.substring(fileName.lastIndexOf("\"));
但它并不健壮和可靠.请问有什么建议吗?
But it's not robust and reliable. Any suggestions please?
推荐答案
Commons IO 提供 FilenameUtils#getName()
用于确切目的.
Commons IO offers FilenameUtils#getName()
for the exact purpose.
String filename = FilenameUtils.getName(event.getFile().getFileName());
另见:
- 关于该主题的 Commons FileUpload 常见问题
- 如何在 JSF 中保存上传的文件
这篇关于event.getFile().getFileName() 使用 PrimeFaces 3.5 在 JSF2.0 中返回带有完整路径的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!