aka*_*okd 81
new File(fileName).getName();
Run Code Online (Sandbox Code Playgroud)
要么
int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;
Run Code Online (Sandbox Code Playgroud)
请注意,第一个解决方案是系统相关的.它只考虑系统的路径分隔符.因此,如果您的代码在Unix系统上运行并且接收Windows路径,那么它将无法工作.处理Internet Explorer发送的文件上载时就是这种情况.
ska*_*man 18
Apache Commons IO提供了FilenameUtils类,它为您提供了一组非常丰富的实用程序函数,可以轻松获取文件名的各种组件,尽管java.io.File类提供了基础知识.
Seb*_*rez 16
来自Apache Commons IO FileNameUtils
String fileName = FilenameUtils.getName(stringNameWithPath);
Run Code Online (Sandbox Code Playgroud)