struts2实现多文件批量上传并手动拦截文件类型和大小

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值