struts 1.3下多文件上传处理
先建一个JSP文件
<html:form action="/CFIS_AlbumAddPhotos.do"enctype="multipart/form-data" method="post">image1 : <html:file property="image1" style/><html:errors property="image" /><br />image2:<html:file property="image2" style/><html:errors property="image" /><br />image3:<html:file property="image3" style/><html:errors property="image" /><br />image4:<html:file property="image4" style/><html:errors property="image" /><br />image5:<html:file property="image5" style/><html:errors property="image" /><br /><html:submit /><html:cancel /></html:form>
<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>
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(); }}