}
public List getMyFiles() {
return myFiles;
}
//注意这个方法的定义 不加中间的循环是会出错的
public UploadFile getUploadFile(int index){
int size = myFiles.size();
if(index>size-1){
for(int i=0;i<index-size+1;i++){
myFiles.add(new UploadFile());
}
}
return (UploadFile)myFiles.get(index);
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
}
基于Struts1.2的动态多文件上传(4)
时间:2011-08-05
最后是action
package com.hb.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
String fileStr = "";
for (int i = 0; i < myFiles.size(); i++) {
UploadFile uploadFile = (UploadFile) myFiles.get(i);
FormFile file = uploadFile.getFile();
if (file == null) {
System.out.println("file is null");
} else {
// 能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
System.out.println("filename>>>>>>>>>>" + file.getFileName());
System.out.println("file size>>>>>>>>>>" + file.getFileSize());
fileStr += "filename:::" + file.getFileName() + "file size:::"
+ file.getFileSize();
}
}
// 返回文本
try {
response.setHeader("ContentType", "text/html;charset=gbk");
PrintWriter out = response.getWriter();
out.write("<script>parent.callback(''upload file success" + fileStr
+ "'')</script>");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
这个是我从现在写好的里截取的一部分,可能还是有不正确的地方,但是大体的思想是在这了 struts1.2 我用的jar包 |