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;

public void setSavePath(String value){
this.savePath=value;
}
public String getSavePath(){
return ServletActionContext.getServletContext().getRealPath("/WEB-INF/"+savePath);
}
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;
}

 注:部分属性的getter和setter方法省略

3、-----------------------------------------------------struts.xml文件内容-------------------------------------------------------

//配置该应用使用的编码格式

<constant name="struts.custom.il8n.resources" value="mess" />
<constant name="struts.il8n.encoding" value="utf-8"></constant>


 <action name="upload" class="com.test.action.uplaodAction">
        <param name="savePath">/upload</param>
        <result name="success">success.jsp</result>
        </action> 


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

注:批量上传和单个文件上传的区别是多了一个for循环


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值