jsp页面内容:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for FileLoadForm form</title>
</head>
<body>
<html:form action="/fileLoad" enctype="multipart/form-data" method="post">
文件2 : <html:file property="file[1]"/><html:errors property="file[1]"/><br/>
文件1 : <html:file property="file[0]"/><html:errors property="file[0]"/><br/>
<html:submit value="点击上传"/><html:cancel value="取消"/>
</html:form>
</body>
</html>
formBean内容:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class FileLoadForm extends ActionForm {
private FormFile[] file;
//----------------------------------
private String filepath;
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
//-----------------------------------
public FileLoadForm(){
file = new FormFile[2];
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public FormFile getFile(int i) {
return file[i];
}
public void setFile( int i,FormFile file) {
this.file[i] = file;
}
}
action的内容:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
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.FileLoadForm;
import com.yourcompany.struts.form.LoadfileForm;
public class FileLoadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
if (form instanceof FileLoadForm) {
String encoding = request.getCharacterEncoding();
if(encoding!=null && encoding.equalsIgnoreCase("utf-8")){
response.setContentType("text/html;charset=utf-8");
}
FileLoadForm fileLoadForm = (FileLoadForm) form;
for(int i=0;i<2;i++){
System.out.println(i);
FormFile loadfile = fileLoadForm.getFile(i);//取得上传文件
try {
InputStream stream = loadfile.getInputStream();//把文件读入
String filePath = this.getServlet().getInitParameter("uploadPath");//取得当前系统路径
String path = filePath+"/"+loadfile.getFileName();
request.getSession().setAttribute("filePath", path);
OutputStream bos = new FileOutputStream(path);//建立一个上传文件输出流
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = stream.read(buffer, 0, 8192))!=-1){
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return mapping.findForward("ok");
}
}