先建一个JSP文件
对应struts配置文件添加代码:
对应Action的代码:
<html:form action="/CFIS_AlbumAddPhotos.do"
enctype="multipart/form-data" method="post">
image1 : <html:file property="image1" styleClass="input" />
<html:errors property="image" />
<br />
image2:<html:file property="image2" styleClass="input" />
<html:errors property="image" />
<br />
image3:<html:file property="image3" styleClass="input" />
<html:errors property="image" />
<br />
image4:<html:file property="image4" styleClass="input" />
<html:errors property="image" />
<br />
image5:<html:file property="image5" styleClass="input" />
<html:errors property="image" />
<br />
<html:submit /><html:cancel />
</html:form>
对应struts配置文件添加代码:
<form-bean name="CFIS_AlbumAddPhotosForm"
type="org.apache.struts.validator.DynaValidatorForm" >
<form-property name="image1" type="org.apache.struts.upload.FormFile"/>
<form-property name="image2" type="org.apache.struts.upload.FormFile"/>
<form-property name="image3" type="org.apache.struts.upload.FormFile"/>
<form-property name="image4" type="org.apache.struts.upload.FormFile"/>
<form-property name="image5" type="org.apache.struts.upload.FormFile"/>
</form-bean>
对应Action的代码:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
albumAddPhotosForm.getImage();
DynaActionForm albumAddPhotosForm = (DynaActionForm) form;
//利用MultipartRequestHandler处理同时上传多个文件
MultipartRequestHandler multipartRequestHandler = albumAddPhotosForm
.getMultipartRequestHandler();
// 取得所有上传文件的对象集合
Hashtable elements = multipartRequestHandler.getFileElements();
// 循环遍历每一个文件
Collection values = elements.values();
int k = 0;
for (java.util.Iterator i = values.iterator(); i.hasNext();)
{
// 取得上传的文件
FormFile file = (org.apache.struts.upload.FormFile) i.next();
//获取文件字节流
byte[] content = file.getFileData();
//以时间格式生成文件名
String url = “C:\\”;
//用输出流保存文件
FileOutputStream fos = new FileOutputStream(url);
//写入时间
fos.write(content);
//关闭输出流
fos.close();
}
}