首先新建一个类UploadFile:
package...;(包名自取)
import java.io.Serializable;
import org.apache.struts.upload.FormFile;
public class UploadFile implements Serializable {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
接着是ActionForm:
import ..UploadFile;(包名自取)
import org.apache.struts.action.*;
public class AddForm extends ActionForm {
private List fileList; // 上传的附件
public AddForm() {
fileList = new ArrayList();
fileList.add(new UploadFile());
}
public UploadFile getUploadFile(int index) {
int size = fileList.size();
for(int i=size;i<=index;i++){
//这里是页面删除一个附件后加入一个新的UploadFile对象,上传处理时判断文件名即可
fileList.add(new UploadFile());
}
return (UploadFile) fileList.get(index);
}
public List getFileList() {
return fileList;
}
public void setFileList(List fileList) {
this.fileList = fileList;
}
}
然后是JSP页面:
文件上传页面
<%@ include file="/include/jsp/pageTopForm.jsp" %>
上传处理的核心代码:(具体的类及Action省略)
for(int i=0;i
UploadFile uploadfile = (UploadFile)pWebForm.getFileList().get(i);
if(uploadfile.getFile()!=null&&!"".equals(uploadfile.getFile().getFileName())){
System.out.println("文件"+(i+1)+"--->"+uploadfile.getFile().getFileName());
//得到UploadFile对象后就可以进行文件上传了,后面的代码省略
}
}
阅读(715) | 评论(0) | 转发(0) |