首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

struts 1.3上多文件下传处理

2012-10-26 
struts 1.3下多文件上传处理先建一个JSP文件html:form action/CFIS_AlbumAddPhotos.doenctypemultip

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>


对应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();        }}         

热点排行