<body>
<form action="myupload.action" method="post" enctype="multipart/form-data">
<table align="center" width="60%" border="1">
<tr>
<td>
选择上传的文件:
</td>
<td id="more">
<input type="file" name="myFile">
<input type="button" value="增加" onclick="addMore()"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="上传" align="center"></s:submit>
</td>
</tr>
</table>
</form>
</body>
</html>
上传多文件时,需要动态生成文件上传表单。
如果需要制定文件上传的最大值,可以在struts.xml文件里面设置:
<!-- 多文件上传 -->
<action name="myupload" class="com.struts2.ManyFileAction">
<param name="savepath">/upload</param>
<result name="success" >/uploadSuc.jsp</result>
</action>
FileInputStream fis = null ;
FileOutputStream fos = null ;
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
savepath = ServletActionContext.getServletContext().getRealPath(savepath) ;
System.out.println("路径:"+savepath);
int size = getMyFile().size() ;
if(size!=0){
for(int i=0;i<size;i++){
fis = new FileInputStream(myFile.get(i)) ;
bis = new BufferedInputStream(fis) ;
byte[] tmp = new byte[bis.available()] ;
fos = new FileOutputStream(new File(savepath+"//"+myFileFileName.get(i))) ;
bos = new BufferedOutputStream(fos) ;
int len =0 ;
while((len=bis.read(tmp))!=-1){
bos.write(tmp, 0, len) ;
}
}
bis.close() ;
bos.close() ;
}
return SUCCESS ;
}
}
至此,文件上传就完成了.