问题描述
我正在使用PrimeFaces v3.5使用Firefox浏览器将文件上传到Windows计算机中. event.getFile().getFileName()
返回具有完整路径的文件名,这进一步导致了问题.内部PrimeFaces使用的是Apache Commons.我检查了 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?
推荐答案
公共IO提供 FilenameUtils#getName()
的确切目的.
Commons IO offers FilenameUtils#getName()
for the exact purpose.
String filename = FilenameUtils.getName(event.getFile().getFileName());
另请参见:
- 在该主题上常见的FileUpload常见问题
- 如何在JSF中保存上传的文件
- Commons FileUpload FAQ on the subject
- How to save uploaded file in JSF
See also:
这篇关于event.getFile().getFileName()返回带有PrimeFaces 3.5的JSF2.0中具有完整路径的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!