1、jsp
jsp里有包含名为"aterialFile”file表单对象,其名字后用“[]”用序号标识顺序,如aterialFile[0]、aterialFile[1]、aterialFile[2]......。课件”按钮动态添加多个课件表单。
< form action = "" method = "post" enctype = "multipart/form-data" >
.....
<input type="button" value="添加课件" id="separateAdd" class="CmdButton" onclick="addPartItem()"> <TABLE width="100%" cellSpacing="0" cellPadding="0" border="0" class="FormTable" style="border-color: #ffffff" id="partList" > <tr> <td width="40" nowrap="nowrap" align="center" >名称</td> <td width="180" align="center" > <input type="text" name="courseWareName" style="width:80%;" /> </td> <td width="200" align="center" > <input type="file" name="materialFile[0]" class="FormInput" style="width:200px;" /> </td> <td width="40" align="center" ><button class="DeleteButton">删除</button></td> </tr> </TABLE>
</form> <div id="itemDiv" style="display: none;"> <tr> <td width="40" nowrap="nowrap" align="center" >名称</td> <td width="180" align="center" > <input type="text" name="courseWareName" style="width:80%;" /> </td> <td width="200" align="center" > <input type="file" name="materialFile" class="FormInput" style="width:200px;" /> </td> <td width="40" align="center" ><button class="DeleteButton">删除</button></td> </tr> </div> <script language="javascript" > function addPartItem(){ $("#partList").append($("#itemDiv").html()); initInputName(); } $(".DeleteButton").live('click',function(event){ $(this).closest("tr").remove(); initInputName(); }); function initInputName(){ var num=0; $("input[name^='materialFile']").each(function(){ $(this).attr("name","materialFile["+num+"]"); num++; }); </script> |
2、ActionForm
public class CourseActionForm extends ActionForm { private static final long serialVersionUID = 1L; /** * 课件名称 */ private String[] courseWareName; /** * 课件ID */ private Integer[] courseWareId; /** * 用于保存不定数量的FormFile对象 */ private Map materialFiles = new HashMap(); public FormFile getFile(int i) // 索引属性 { return (FormFile) materialFiles.get(new Integer(i)); } public void setMaterialFile(int i, FormFile formFile) // 索引属性 { if (formFile.getFileSize() > 0) {// 只有上传文件的字节数大于0,才上传这个文件 materialFiles.put(new Integer(i), formFile); } } public String[] getCourseWareName() { return courseWareName; } public void setCourseWareName(String[] courseWareName) { this.courseWareName = courseWareName; } public int getFileCount() // 获得上传文件的个数 { return materialFiles.size(); } public void reset(ActionMapping mapping, HttpServletRequest request) { materialFiles.clear(); } public Integer[] getCourseWareId() { return courseWareId; } public void setCourseWareId(Integer[] courseWareId) { this.courseWareId = courseWareId; } |
该ActionForm最主要的特点是setMaterialFile方法,其中有两个参数,第一个参数代表上传文件的索引,第二个参数代表该文件对象。其中方法的名字很关键,set后的“MaterialFile”和表单的文件名字除[]的名字“materialFile”一致。通过该方法将formFile对象放在一个Map中方便与名字对应。
3、Action
public class CreateCourseAction extends ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ........ for(int i=0;i<courseForm.getFileCount();i++){ FormFile materialFileFormFile = courseForm.getFile(i);//通过此方法可以获取单个formFile值 .......... } } |