情景:
上傳圖片,我們經常會對圖片的後綴進行判斷,比如獲取文件名,進行截取文件後綴名 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")