界面:
<span style="font-size:18px;"> <body>
<h2>使用Struts上传文件的功能</h2>
<form action="struts_fileupload.do" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>请选择要上传的文件</td>
<td><input type="file" name="filePath" size="50"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="上传"></td>
</tr>
</table>
</form>
</body></span>
配置文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="fileUploadForm" type="com.java.struts.actionForm.FileUploadActionForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/struts_fileupload"
name="fileUploadForm"
type="com.java.struts.action.FileUploadAction"
scope="request">
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
</span>
逻辑处理类:
<span style="font-size:18px;">package com.java.struts.action;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
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.java.struts.actionForm.FileUploadActionForm;
public class FileUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileUploadActionForm myForm = (FileUploadActionForm) form;
FormFile fileObj = myForm.getFilePath();
//得到存放上传文件的目录的真实路径
String dir = this.getServlet().getServletContext().getRealPath("/upload");
//如果没有选择文件
if(fileObj==null) {
return mapping.findForward("success");
}
//获得上传的文件名
String fileName = fileObj.getFileName();
//获得上传的文件大小
int size = fileObj.getFileSize();
if(size > 1024 * 1024) {
return mapping.findForward("success");
}
//从上传文件中得到一个输入流...之后就可以进行相应操作了
InputStream is = fileObj.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dir + "/" + fileName));
byte[] buffer = new byte[20480];
int count = 0;
while((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
is.close();
return mapping.findForward("success");
}
}
</span>
actionform:
<span style="font-size:18px;">package com.java.struts.actionForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileUploadActionForm extends ActionForm {
//文件上传输入域在ActionForm中必须定义为FormFile类型
private FormFile filePath;
public FormFile getFilePath() {
return filePath;
}
public void setFilePath(FormFile filePath) {
this.filePath = filePath;
}
}</span>