对于上传的文件,我们怎么知道类型?如果用Spring提供的MultipartFile,这里可以获取ContentType来判断,这里只提供思路
/**文件类型集合*/ private static Map<String,String> FILE_TYPES =new HashMap<String,String>(); /**图片类型集合*/ private static Map<String,String> IMG_TYPES = new HashMap<String,String>(); static{ FILE_TYPES.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//xlsx FILE_TYPES.put("xls", "application/vnd.ms-excel");//xls FILE_TYPES.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");//docx FILE_TYPES.put("doc", "application/msword");//doc FILE_TYPES.put("jpg", "image/jpeg");//jpg FILE_TYPES.put("png", "image/png");//png FILE_TYPES.put("gif", "image/gif");//gif FILE_TYPES.put("bmp", "image/bmp");//bmp FILE_TYPES.put("txt", "text/plain");//txt FILE_TYPES.put("pdf", "application/pdf");//pdf FILE_TYPES.put("zip", "application/x-zip-compressed");//zip FILE_TYPES.put("rar", "application/octet-stream");//rar } static{ IMG_TYPES.put("jpg", "image/jpeg");//jpg IMG_TYPES.put("png", "image/png");//png IMG_TYPES.put("gif", "image/gif");//gif IMG_TYPES.put("bmp", "image/bmp");//bmp } /** * 校验上传附件是否为图片类型的 * @author nicky.ma * @date 2019年6月12日上午11:15:30 * @param fileContentType * response格式ContentType */ public static boolean checkContainImgType(String fileContentType){ if(!StringUtils.isEmpty(fileContentType)){ return IMG_TYPES.containsValue(fileContentType); } return false; } public static String loadFileType(String fileContentType){ for(Map.Entry<String, String> entry : FILE_TYPES.entrySet()){ if(entry.getValue().equals(fileContentType)){ return entry.getKey(); } } return null; }图片添加二维码水印教程 (3)
内容版权声明:除非注明,否则皆为本站原创文章。