首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts 一 中服务器端限制文件上传的类型及扩展名

2013-03-27 
struts 1 中服务器端限制文件上传的类型及扩展名src下建个properties文件,放置允许上传的文件类型:allowup

struts 1 中服务器端限制文件上传的类型及扩展名

src下建个properties文件,放置允许上传的文件类型:
allowuploadfiletype.propertiesgif=image/gifjpg=image/jpg,image/jpeg,image/pjpegbmp=image/bmppng=image/pngswf=application/x-shockwave-flashdoc=application/mswordtxt=text/plainxls=application/vnd.ms-excelppt=application/vnd.ms-powerpointpdf=application/pdfexe=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;    }}

?

热点排行