Struts upload组件同一个页面中多文件上传
2006-06-27 16:52
375 查看
jsp代码如下:
<html:form method="post" action="/upload.do" enctype="multipart/form-data">
<table width="75%" border="1" align="center">
<tr>
<td>
<div align="center">
1、
<html:file property="file1" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
2、
<html:file property="file2" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
3、
<html:file property="file3" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
4、
<html:file property="file4" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
<html:submit>上传它!</html:submit>
</div>
</td>
</tr>
</table>
</html:form>
struts配置文件中配置动态ActionForm如下:
<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="file3" type="org.apache.struts.upload.FormFile" />
<form-property name="file1" type="org.apache.struts.upload.FormFile" />
<form-property name="file4" type="org.apache.struts.upload.FormFile" />
<form-property name="file2" type="org.apache.struts.upload.FormFile" />
</form-bean>
Action中处理上传的代码如下:
DynaActionForm upForm = (DynaActionForm) form;
String dir = this.getServlet().getServletContext().getRealPath("/upload/uploaded");
FormFile fileOne = (FormFile)upForm.get("file1");
FormFile fileTwo = (FormFile)upForm.get("file2");
FormFile fileThree = (FormFile)upForm.get("file3");
FormFile fileFour = (FormFile)upForm.get("file4");
FormFile[] files = new FormFile[]{fileOne, fileTwo, fileThree, fileFour};
List fileList = new ArrayList();
for(int i = 0;i<files.length;i++){
if(files[i] == null)
continue;
String fileName = files[i].getFileName();
int size = files[i].getFileSize();
try {
InputStream streamIn = files[i].getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[4096];
while((bytesRead = streamIn.read(buffer, 0, 4096)) != -1)
streamOut.write(buffer, 0, bytesRead);
streamOut.close();
streamIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.fail");
msgs.add("error.upload.fail",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
} catch (IOException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.fail");
msgs.add("error.upload.fail",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}
fileList.add(fileName);
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.success");
msgs.add("error.upload.success",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}
<html:form method="post" action="/upload.do" enctype="multipart/form-data">
<table width="75%" border="1" align="center">
<tr>
<td>
<div align="center">
1、
<html:file property="file1" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
2、
<html:file property="file2" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
3、
<html:file property="file3" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
4、
<html:file property="file4" size="30"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
<html:submit>上传它!</html:submit>
</div>
</td>
</tr>
</table>
</html:form>
struts配置文件中配置动态ActionForm如下:
<form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="file3" type="org.apache.struts.upload.FormFile" />
<form-property name="file1" type="org.apache.struts.upload.FormFile" />
<form-property name="file4" type="org.apache.struts.upload.FormFile" />
<form-property name="file2" type="org.apache.struts.upload.FormFile" />
</form-bean>
Action中处理上传的代码如下:
DynaActionForm upForm = (DynaActionForm) form;
String dir = this.getServlet().getServletContext().getRealPath("/upload/uploaded");
FormFile fileOne = (FormFile)upForm.get("file1");
FormFile fileTwo = (FormFile)upForm.get("file2");
FormFile fileThree = (FormFile)upForm.get("file3");
FormFile fileFour = (FormFile)upForm.get("file4");
FormFile[] files = new FormFile[]{fileOne, fileTwo, fileThree, fileFour};
List fileList = new ArrayList();
for(int i = 0;i<files.length;i++){
if(files[i] == null)
continue;
String fileName = files[i].getFileName();
int size = files[i].getFileSize();
try {
InputStream streamIn = files[i].getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[4096];
while((bytesRead = streamIn.read(buffer, 0, 4096)) != -1)
streamOut.write(buffer, 0, bytesRead);
streamOut.close();
streamIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.fail");
msgs.add("error.upload.fail",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
} catch (IOException e) {
e.printStackTrace();
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.fail");
msgs.add("error.upload.fail",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}
fileList.add(fileName);
ActionMessages msgs = new ActionMessages();
ActionMessage msg = new ActionMessage("error.upload.success");
msgs.add("error.upload.success",msg);
request.setAttribute(Globals.MESSAGE_KEY,msgs);
}
相关文章推荐
- Struts upload组件同一个页面中多文件上传
- Struts 的文件上传组件org.apache.struts.upload
- 在jsp中使用smartupload组件上传文件
- ASPUpload组件上传文件带进度
- JS组件系列——自己封装一个上传文件组件
- 使用SmartUpload组件实现文件上传
- 一个关于用Struts实现文件上传异常的解决
- 使用jspSmartUpload组件进行文件上传、下载、批量下载
- chunkupload文件上传断点续传组件(java)
- 利用flex+sevlet+fileupload组件实现文件的上传和下载
- java基础篇---文件上传(smartupload组件)
- jquery uploadify for asp 批量文件上传程序 整合风声asp无组件上传
- 一个简单的struts实现文件上传示例
- 采用jspSmartUpload组件进行文件的上传
- Vue封装一个简单轻量的上传文件组件的示例
- element-ui upload组件多文件上传
- jspSmartUpload--文件上传与下载组件的应用----文件上传、下载、删除
- AjaxFileUpload文件上传组件(php+jQuery+ajax)
- elemetUi 组件--el-upload实现上传Excel文件的实例
- 使用struts upload包 进行文件上传的例子