我正在使用Derby作为数据库在Eclipse IDE中使用JSF开发应用程序。我具有将文件上传到数据库的功能。但是文件名存储为“ C:\ Documents and Settings \ Angeline \ Desktop \ test.txt”而不是“ test.txt”。如何仅将“ test.txt”作为文件名存储在数据库中?

这是我在JSF中的代码:

 File to Upload:

<t:inputFileUpload id="fileupload" value="#{employeeBean.upFile}" storage="file"/>


Java Bean代码:

 String fileName=upFile.getName();


此fileName的值= C:\ Documents and Settings \ Angeline \ Desktop \ test.txt。

最佳答案

lastSlashIndex = name.lastIndexOf("\\");
if (lastSlashIndex == -1) {
    lastSlashIndex = name.lastIndexOf("/"); //unix client
}
String shortName = name;
if (lastSlashIndex != -1) {
    shortName = name.substring(lastSlashIndex);
}


请注意,如果* nix上的文件名包含\,则将无法使用。

07-27 13:14