struts的多文件上传
来源:互联网 发布:锦尚中国源码 编辑:程序博客网 时间:2024/05/22 00:55
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="uploadImages">
<input type="file" name="uploadImages">
</form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class uploadAction{
private File[] uploadImages;//得到上传的文件
private String[] uploadImagesContentType;//得到文件的类型
private String[] uploadImagesFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String saveFiles() throws Exception{
ServletContext sc = ServletActionContext.getServletContext();
String realpath = sc.getRealPath("/uploadfile");
try {
if(uploadImages!=null&&uploadImages.length>0){
for(int i=0;i<uploadImages.length;i++){
File destFile = new File(realpath,uploadImageFileNames[i]);
FileUtils.copyFile(uploadImages[i], destFile);
}
}
} catch (IOException e) {
e.printStackTrace();}return "success";}}
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="uploadImages">
<input type="file" name="uploadImages">
</form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
public class uploadAction{
private File[] uploadImages;//得到上传的文件
private String[] uploadImagesContentType;//得到文件的类型
private String[] uploadImagesFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String saveFiles() throws Exception{
ServletContext sc = ServletActionContext.getServletContext();
String realpath = sc.getRealPath("/uploadfile");
try {
if(uploadImages!=null&&uploadImages.length>0){
for(int i=0;i<uploadImages.length;i++){
File destFile = new File(realpath,uploadImageFileNames[i]);
FileUtils.copyFile(uploadImages[i], destFile);
}
}
} catch (IOException e) {
e.printStackTrace();}return "success";}}
0 0
- struts的多文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- Struts的文件上传
- struts的文件上传
- struts的文件上传
- struts的文件上传
- struts的文件上传
- struts 的文件上传
- struts文件的上传
- Bootloader的作用
- AC自动机
- ArrayList练习2
- Monkey常见面试问题
- 小记(一):字符串与字符串数组之间的转换
- struts的多文件上传
- Python发送邮件
- 集合框架(HashSet)
- struts2的国际化
- Spring Security3学习笔记
- 基于libevent2.0的ministored的http框架--client端
- hdoj 1431 素数回文
- 集合框架(HashSet存储自定义对象)
- 基于libevent2.0的ministored的http框架--server端