[踩过的坑]图片上传,判断图片类型
程序员文章站
2024-03-13 09:26:41
...
情景:
上传图片,我们经常会对图片的后缀进行判断,比如获取文件名,进行截取文件后缀名 jpg,png等等
如果用户把一个文本文件后缀改为.png格式。那么 对文件后缀名判断就不正确了。
清单一:根据文件名后缀判断图片类型
MultipartFile coverFile = (MultipartFile) request.getFile(fileId);
String coverType = coverFile.getOriginalFilename().
substring(coverFile.getOriginalFilename().lastIndexOf(".") + 1).
toLowerCase();
if (coverType == "jpeg" || coverType == "gif" || coverType == "png" || coverType == "bmp" || coverType == "jpg")
清单二:解析上传文件类型
MultipartFile imageFile = (MultipartFile) request.getFile("file");
String imageType = imageFile.contentType
if (imageType == "image/jpeg" || imageType == "image/gif" || imageType == "image/png" || imageType == "image/bmp")