out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public String execute() throws Exception {
File[] srcFiles = this.getUpload();
// 处理每个要上传的文件
for (int i = 0; i < srcFiles.length; i++) {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.getServletContext()
.getRealPath(this.getSavePath())
+ "\\" + this.getUploadFileName()[i];
File dstFile = new File(dstPath);
this.copy(srcFiles[i], dstFile);
}
returnSUCCESS;
}
}
运行结果:
5. Struts2的文件下载:
文件下载相对简单一些,一般只需在页面上提供一个超链接,该链接的href属性等于要下载文件的文件名就行了。但当文件名有中文时,就会导致失败;或者要在用户下载前进行权限判断,这时用Struts2提供的文件下载功能就能简单的解决这些问题。

