struts2实现多文件批量上传并手动拦截文件类型和大小
来源:互联网 发布:南京交通驾校 知乎 编辑:程序博客网 时间:2024/06/14 21:39
1、------------------------------------------jsp页面---------------------------------------------------------------
<s:form action="upload" name="upForm" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="文件标题" /><br/>
<s:file name="file" label="选择文件1" />
<s:file name="file" label="选择文件2" />
<s:file name="file" label="选择文件3" />
<s:submit value="上传"/>
</s:form>
2、-----------------------------------------action类---------------------------------------------------------------
public class uplaodAction extends ActionSupport{
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
//这个属性是配置struts.xml文件来注入的
private String savePath;
//这个属性用来存放所有允许上传的文件类型的后缀
private String allowTypes;
//这个属性用来存放限制上传文件的大小的值
private long allowSize;
public String execute() throws Exception {
System.out.println("看看路径"+getSavePath());
try{
//循环写入
for(int i=0;i<file.size();i++){
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getFileFileName().get(i));
//建立输入流
FileInputStream fis=new FileInputStream(file.get(i));
//建立缓冲区
byte[] buffer=new byte[1024];
int length=0;
//循环从缓冲区读取内容到输入流中
while((length=fis.read(buffer))>0){
fos.write(buffer, 0, length);
}
}
}catch(Exception e){
e.printStackTrace();
}
return SUCCESS;
}
//实现上传文件过滤
public String filterTypesAndSize(String[] types,Long size){
System.out.println("fileterTypesAndSize");
List<String> fileTypes=this.getFileContentType();
if(fileTypes.size()>0 ){
for(int i=0;i<fileTypes.size();i++){
if(this.getFile().get(i).length()<size){
for(int j=0;j<types.length;j++){
if(fileTypes.get(i).equals(types[j])){
return null;
}
}
}
}
}
return ERROR;
}
public String fileter(String[] types){
List<String> upFileTypes=this.getFileContentType();
for(String type : types){
for(String upfiletype : upFileTypes){
if(type.equals(upfiletype)){
return null;
}
}
}
return ERROR;
}
//用Strust2的输入校验来判断用户上传的文件是否是允许上传的类型
public void validate(){
System.out.println("validate");
if(this.getFile() != null){
for(File uploadFile : this.getFile()){
//将允许上传文件类型的字符串以逗号(,)分解成字符串数组,从而判断当前文件类型是否允许上传
//String filterResult = this.filterTypesAndSize(this.getAllowTypes().split(","),allowSize);
String filterResult=this.fileter(this.getAllowTypes().split(","));
if(filterResult != null){
addFieldError("msg", "你要上传的文件类型不正确或大小超出限制");
}
}
}
}
注:属性的setter和getter方法省略
3、----------------------------------------struts.xml文件------------------------------------------------------------------------------------------------------
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.il8n.resources" value="mess" />
<constant name="struts.il8n.encoding" value="utf-8"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.test.action.uplaodAction">
<param name="savePath">/upload</param>
<param name="allowTypes">image/png,image/jpg,image/gif,image/jpeg</param>
<param name="allowSize">10240000</param>
<result name="success">success.jsp</result>
<result name="input" >/WEB-INF/error.jsp</result>
</action>
</package>
- struts2实现多文件批量上传并手动拦截文件类型和大小
- Struts2实现文件批量上传
- struts2实现文件上传(类型、大小过滤——手动、拦截器)
- struts2实现文件批量上传功能
- struts2的文件上传(2):手动实现上传文件类型的过滤
- Struts2 文件上传 文件类型 大小过滤
- struts2 文件上传check文件类型大小
- Flex实现多文件批量上传
- Flex实现多文件批量上传
- Flex实现多文件批量上传
- fileupload实现多文件批量上传
- struts2文件批量上传
- python和javascript实现文件批量上传
- JS限制上传文件类型和大小
- 上传文件 限制文件类型和大小
- 图片上传 文件类型和大小的判断
- Struts2 上传文件类型限制(zip和rar)
- Struts2 文件上传文件名和文件类型
- linux下vim命令详解
- 详解排列组合
- ubuntu清理磁盘
- 网站 IIS线程池 处理
- 第十七周上机实践项目——年龄几何
- struts2实现多文件批量上传并手动拦截文件类型和大小
- V4L2视频采集操作流程和接口说明
- linux触摸屏驱动开发中的s3c_ts_probe()函数的分析
- pysnmp安装
- 忘记该忘记的
- 比较好的网页视频播放器总结
- C++得到当前进程所占用的内存
- hdu1551Cable master(二分,精度)
- duilib进阶教程 -- 改进List控件 (1)