转载:
http://hi.baidu.com/javajavajava/blog
src下建个properties文件,放置允许上传的文件类型:
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文件上传大小限制问题struts文件上传大小限制问题struts文件上传大小限制问题struts文件上传大小限制问题
struts1的服务器端验证,一个很好的验证配置文件,线程安全问题有保障
Struts1的fileupload的文件上传
struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传struts1.x实现多文件上传
struts1实现的文件批量上传,java实现
struts1 实现文件上传 不喜勿喷 谢谢 合作
Struts1 实现的文件上传 有分页技术 不过没有下载 可以实现的一个小项目 很适合入门Struts1 或者说是想了解Struts1文件上传的新手的一个项目
使用struts1实现文件上传和下载,上传有单文件上传,多文件上传,动态添加上传文件域。
struts2实现服务器端校验示例,包含Dojo实现类似ajax校验
在struts2基础上实现的包括单文件、多文件的上传及下载
struts1和struts2分别实现文件上传下载功能
JavaEE Struts文件上传
NULL 博文链接:https://yongjie.iteye.com/blog/792673
struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传
struts 单文件上传和多文件上传带进度条
Struts1文件上传、单文件、多文件上传(上传文件大小问题,如何设置无限制)
struts1实现文件上传和下载,struts1实现文件上传和下载。
1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.能够对上传路径进行配置文件指定(upload.properties),使用了一些类似单例模式的静态代码块 3.Struts2进行下载处理,能对上传的所有...
struts 文件上传 Struts实现文件上传