struts 1 中服务器端限制文件上传的类型及扩展名
2008-10-11 13:39
519 查看
src下建个properties文件,放置允许上传的文件类型:
allowuploadfiletype.properties
在BaseForm 里面写具体的验证方法:
allowuploadfiletype.properties
gif=image/gif jpg=image/jpg,image/jpeg,image/pjpeg bmp=image/bmp png=image/png swf=application/x-shockwave-flash doc=application/msword txt=text/plain xls=application/vnd.ms-excel ppt=application/vnd.ms-powerpoint pdf=application/pdf exe=application/octet-stream
在BaseForm 里面写具体的验证方法:
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Properties; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class BaseForm extends ActionForm { private static Properties properties = new Properties(); static{ try { properties.load(BaseForm.class.getClassLoader().getResourceAsStream("allowuploadfiletype.properties")); } catch (IOException e) { e.printStackTrace(); } } /** * 获取文件扩展名 * @param formfile * @return */ public static String getExt(FormFile formfile) { return formfile.getFileName().substring( formfile.getFileName().lastIndexOf('.') + 1).toLowerCase(); } /** * 验证上传文件是否属于图片/flash动画/word文件/exe文件/pdf文件/TxT文件/xls文件/ppt文件 * * @param formfile * @return */ public static boolean validateFileType(FormFile formfile) { if (formfile != null && formfile.getFileSize() > 0) { String ext = getExt(formfile); List<String> allowType = new ArrayList<String>(); for (Object key : properties.keySet()) { String value = (String) properties.get(key); String[] values = value.split(","); for (String v : values) { allowType.add(v.trim()); } } return allowType.contains(formfile.getContentType().toLowerCase()) && properties.keySet().contains(ext); } return true; } }
相关文章推荐
- struts 1 中服务器端限制文件上传的类型及扩展名
- struts中上传文件的代码(没有限制上传文件类型)
- PHP图片文件上传类型限制扩展名限制大小限制与自动检测目录创建。
- Struts文件上传的大小及类型的限制
- struts中上传文件的代码(没有限制上传文件类型)
- struts2文件上传类型限制 之 zip和rar文件类型
- struts文件上传,获取文件名和文件类型
- SpringMVC全局文件上传类型限制【终极解决方案】
- 限制上传文件类型的两种方法
- struts文件上传,获取文件名和文件类型
- Web开发之input file控件限制上传文件类型
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
- Uploadify 3.2上传文件,限制类型,大小,传递参数等
- Struts 2实现文件的上传下载(解除文件大小限制)
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
- JS判断限制上传文件(图片)类型
- struts2上传文件类型限制
- [导入]限制上传文件类型的两种方法
- input file控件限制上传文件类型
- js限制上传文件的类型和大小