JavaScript+Struts2实现可选择性多文件上传

页面代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<SCRIPT type="text/javascript">
function addMore(){
var td = document.getElementById("more");
var br = document.createElement("br");//创建一个换行标签
//创建一个类型为type的input标签
var input = document.createElement("input");
input.type="file";//设置input类型为file
input.name="upload"//设置input的名称为upload
//创建一个类型为button的input标签
var button = document.createElement("input");
button.type="button"; //设置input类型为button
button.value="Remove"; //设置input显示为Remove
//设置按钮事件
button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</SCRIPT>
</head>
<body>
<form action="uploadfile4" method="post" enctype="multipart/form-data">
<table border="1px">
<tr>
<td>
file
</td>
<td id="more">
<input type="file" name="upload" />
<input type="button" value="Add More..." onclick="addMore()"/>
</td>
</tr>
<tr align="right">
<td colspan="2">
<input type="submit" value="上传"/>
</td>
</tr>
</table>
</form>
</body>
</html>
当多文件上传时,在Struts2的Action类中可以使用数组也可以使用List<E>泛型的方式来获取上传的多文件,获取数组后,处理方式就和单个文件上传的方式一样
Action处理代码:
private File[] upload; // 上传的文件,与页面的<input type="file"
// name="upload"/>中的name属性一致
private String[] uploadFileName;// 文件名,Struts规定个格式 [name]FileName
private String[] uploadContentType;// 文件类型,Struts规定的格式[name]ContentType
@SuppressWarnings("deprecation")
public String execute() {
try {
for (int i = 0; i < upload.length; i++) {
InputStream is = new FileInputStream(upload[i]);// 创建输入流,读取上传文件
// String parentDir =
// ServletActionContext.getServletContext().getRealPath(parentDir);
String parentDir = ServletActionContext.getRequest()
.getRealPath("/upload");// 指定文件上传目录
File destFile = new File(parentDir, this.getUploadFileName()[i]);
if (!destFile.getParentFile().exists()) {// 判断存放文件的目录是否存在
destFile.getParentFile().mkdir();// 不存在则创建
}
OutputStream os = new FileOutputStream(destFile);// 创建输出流,将内容写入文件
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {// 读文件
os.write(buffer, 0, length);// 写文件
}
is.close();// 关闭inputStream
os.close();// 关闭outputStream
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}

也可以使用泛型的方式进行接收,并且处理:
private List<File> upload;  //上传的文件,与页面的<input type="file" name="upload"/>中的name属性一致
private List<String> uploadFileName;//文件名,Struts规定个格式 [name]FileName
private List<String> uploadContentType;//文件类型,Struts规定的格式[name]ContentType
@SuppressWarnings("deprecation")
public String execute() {
try {
for(int i=0;i<upload.size();i++){
InputStream is = new FileInputStream(upload.get(i));//创建输入流,读取上传文件
//String parentDir = ServletActionContext.getServletContext().getRealPath(parentDir);
String parentDir = ServletActionContext.getRequest().getRealPath(
"/upload");//指定文件上传目录
File destFile = new File(parentDir, this.getUploadFileName().get(i));
if (!destFile.getParentFile().exists()) {//判断存放文件的目录是否存在
destFile.getParentFile().mkdir();//不存在则创建
}
OutputStream os = new FileOutputStream(destFile);//创建输出流,将内容写入文件
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {// 读文件
os.write(buffer, 0, length);// 写文件
}
is.close();//关闭inputStream
os.close();//关闭outputStream
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
只是对多个文件进行遍历,而处理的方式和单个文件是一致的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值