读书人

验证图片下传

发布时间: 2012-12-22 12:05:07 作者: rapoo

验证图片上传

/***
? * 验证图片上传
? * @param forefile
? * @return
? */
?public static boolean validateImageFileType(FormFile formfile){
??if(formfile != null && formfile.getFileSize() > 0){
???List<String> arrowType = Arrays.asList("image/gif","image/jpg","image/bmp","image/png","image/jpeg","image/pjpeg");
???String ext = formfile.getFileName().substring(formfile.getFileName().lastIndexOf(".")+1).toLowerCase();
???List<String> arrowExtension = Arrays.asList("gif","jpg","bmp","png");
???return arrowType.contains(formfile.getContentType().toLowerCase())&&arrowExtension.contains(ext);
??}
??return true;
?}

?

?

?

/***
? * 验证上传文件类型是否属于图片格式(利用反射技术实现)
? * @param propertyName Brand中的属性名称
? * @return
? * @throws Exception
? */
?/*
?public staticboolean validateFileType(String propertyName) throws Exception{
??PropertyDescriptor[] propertydesc = Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors();
??boolean exsit = false;
??for(PropertyDescriptor property : propertydesc){
???if(property.getName().equals(propertyName)){
????Method method = property.getReadMethod();
????if(method != null){
?????exsit = true;
?????FormFile forefile = (FormFile)method.invoke(this);
?????if(forefile != null && forefile.getFileSize() > 0){
??????List<String> arrowType = Arrays.asList("image/gif","image/jpg","image/bmp","image/png","image/jpeg","image/pjpeg");
??????return arrowType.contains(forefile.getContentType().toLowerCase());
?????}
????}else{
?????new RuntimeException(propertyName + "属性getter方法不存在");
????}
???}
??}
??if(!exsit)new RuntimeException(propertyName + "属性不存在");
??return true;
?}*/

读书人网 >编程

热点推荐