在 struts1 和 2 中,分别对 commons-fileupload 组件进行了包装,使文件上传操作更方便。
本文讲解如何使用 struts1 上传文件。
步骤:
1 编写 jsp 页面
2 给 jsp 中的 form 表单配置一个 ActionForm
3 编写 Action ,处理文件上传功能
4 配置 struts-config.xml 文件
5 测试
1、 首先有个 jsp 页面 —upload_form.jsp
<%@ page language = "java" contentType = "text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1" %> <%@ taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > < title > Upload File </ title > </ head > < body > < html:form action = " upload.do " method = "post" enctype = " multipart/form-data " > < html:file property = " file " ></ html:file > < input type = "submit" value = "Submit" /> </ html:form > </ body > </ html > |
其中的 action 是指提交到的 action
enctype 是指表单提交的数据格式
2 给上面的 jsp 中表单配一个 ActionForm –UploadForm
public class UploadForm extends ActionForm { private static final long serialVersionUID = 1L;
private FormFile file ; public FormFile getFile() { return file ; } public void setFile(FormFile file) { this . file = file; } } |
其中的 file 成员变量对应 upload_form.jsp 表单中为 file 的 property 。
在 struts1 中,文件对象被封装成了 org.apache.struts.upload.FormFile 对象。
3 编写 action –UploadAction
继承 org.apache.struts.action.Action 类
public class UploadAction extends Action { Logger logger = Logger.getLogger (UploadAction. class );
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// 将缓存表单数据的 form 强转成 UploadForm 类型,得到其中的 file 变量 UploadForm uf = (UploadForm) form; FormFile ff = uf.getFile();
// 输出 UploadForm 中的变量 file 的信息 - 文件名 - 文件大小 logger .debug( "file name : " + ff.getFileName()); logger .debug( "file size : " + ff.getFileSize());
// 得到该文件的输入流 InputStream is = ff.getInputStream();
// 该文件要保存的路径 - 文件名 , “upload” 为 web-root 下的一文件夹名, // getServlet().getServletContext().getRealPath("upload") 得到 "upload" 文件夹在服务器端的相对位置 // ff.getFileName() 为保存时的文件名 File file = new File(getServlet().getServletContext().getRealPath( "upload" ), ff.getFileName());
logger .debug(file.getPath());
// 创建一个输出流 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(file));
// 从输入流中读取字节,通过输出流写入 file 文件中 int b = -1; while ((b = is.read()) != -1) { bos.write(b); } bos.close(); is.close(); return mapping.findForward( "success" ); } } |
4 编写配置文件 struts-config.xml
<? xml version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" > < struts-config > < data-sources /> < form-beans > < form-bean name = "uploadForm" type = "zl.form.UploadForm" /> </ form-beans > < action-mappings > < action path = "/upload" type = "zl.action.UploadAction" name = "uploadForm" > </ action > </ action-mappings > </ struts-config >
|
其中 /upload 是 upload_form.jsp 提交到的地址相对应。
5 运行
运行成功, console 会打印所上传文件的 name size path 的值。
21:21:30,859 DEBUG UploadAction:33 - file name : Eclipse报表工具.bmp 21:21:30,859 DEBUG UploadAction:34 - file size : 1026198 21:21:30,859 DEBUG UploadAction:45 - D:/Program Files/tomcat6.0/webapps/test2/upload/Eclipse报表工具.bmp |