我正在使用 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();
这个文件名的值=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 上的文件名包含 \
这将不起作用。
关于java - 如何从 java 或 jsf 中的字符串中获取所需的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2354943/