java开发中截取上传文件的文件名和后缀名
/**
* Return the extension portion of the file's name .
*
* @see #getExtension
*/
public static String getExtension(File f) {
return (f != null) ? getExtension(f.getName()) : "";
}
public static String getExtension(String filename) {
return getExtension(filename, "");
}
public static String getExtension(String filename) {
return getExtension(filename, "");
}
public static String getExtension(String filename, String defExt) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i >-1) && (i < (filename.length() - 1))) {
return filename.substring(i + 1);
}
}
return defExt;
}
public static String trimExtension(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i >-1) && (i < (filename.length()))) {
return filename.substring(0, i);
}
}
return filename;
}
------以上代码仅供参考-----