upload.jsp是这样子的>>>
<html:form action="/upload" enctype="multipart/form-data">
<html:text property="title"/><br>
<input type="file" name="testFile(0)"/><br> (可在此增加上传个数)
<input type="file" name="testFile(1)"/><br>
<html:submit value="upload"/>
</html:form>
UploadForm.java
import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private String title;
private ArrayList formFiles = new ArrayList();
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public void setTestFile(String iIndex, FormFile formFile) { this.formFiles.add(formFile); }
public FormFile getTestFile(String iIndex) { return (FormFile) formFiles.get(Integer.parseInt(iIndex)); }
public ArrayList getTestFiles() { return this.formFiles; }
}
UploadAtionAction.java
import java.io.FileOutputStream;
import java.io.InputStream;import java.io.OutputStream;
import java.util.ArrayList;
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;
import com.yourcompany.struts.form.UploadForm;
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
String name=null;
String path = null;
FormFile file;
ArrayList list= new ArrayList();
UploadForm uploadForm = (UploadForm) form; list = uploadForm.getTestFiles();
// FormFile file= uploadForm.getTestFile("0");
// name = file.getFileName();
path=servlet.getServletContext().getRealPath("/upload"); //
String realPath=path+"/"+fname;
try{
for(int j=0; j < list.size();j++){
file= uploadForm.getTestFile(String.valueOf(j));
name = file.getFileName();
if(name != null && !"".equals(name)){
InputStream streamin=file.getInputStream();
OutputStream streamout=new FileOutputStream(path+"/"+name);
byte[] buff=new byte[8192];
int i=0;
while((i=streamin.read(buff,0,8192))>0){
streamout.write(buff, 0, i);
}
streamin.close();
streamout.flush();
streamout.close();
file.destroy(); } }
}catch(Exception e){
e.printStackTrace();
} return null; }
}
至于怎么动态增加或删除上传文件个数,这里就不详讲了,CSDN的博客怎么东西一贴上去,格式就全不见了,所有文字都缩在一起了,好辛苦啊还得手动排版.忍不住,对着屏幕伸出了邪恶的中指...