在文件上传时fileName.endsWith()的获取后悔

endsWith()方法可以获取文件的后缀名,这样就可以通过后缀名来获取想要的文件格式了!

if(!(fileName.endsWith("jpg") || fileName.endsWith("jpeg") || fileName.endsWith("png") || fileName.endsWith("bmp") || fileName.endsWith("gif"))){ return “上传的文件是jpg/jpeg/png/bmp/gif的图片文件!” }

底层用的是:进行了自动截取,获取后缀名
return startsWith(suffix, value.length - suffix.value.length);
调用了startWith()方法
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
posted @ 2021-01-06 12:40  六月的雨莹  阅读(1577)  评论(0编辑  收藏  举报