<!----><!----><!----> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1318531534; mso-list-type:hybrid; mso-list-template-ids:1234214414 225203862 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> <!---->
采用 Struts 可以很方便的实现文件上传工作
<!---->1、 <!---->上传的页面, upload.jsp :
<li>测试struts文件上传</li><br> <form action="upload.do" method="post" enctype="multipart/form-data"> 文件名:<input type="text" name="title"><br> 文件:<input type="file" name="myfile"><br> <input type="submit" value="上传"> </form>
enctype="multipart/form-data" 就是把你表单的所有信息以流方式提交,页面上的所有信息已经都转换为了文件流,为的是能让服务端得到你上传的文件的文件流。 文件项的 type 必须为 file
2 、建立 ActionForm( 注意:必须要用 FormFile 来接收上传的文件 ) , UploadActionForm.java :
package cn.huan.struts; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; /** * 文件上传ActionForm * @author 刘银环 * */ public class UploadActionForm extends ActionForm { //文件名 private String title; //必须要用FormFile private FormFile myfile; /** * @return the title */ public String getTitle() { return title; } /** * @param title the title to set */ public void setTitle(String title) { this.title = title; } /** * @return the myfile */ public FormFile getMyfile() { return myfile; } /** * @param myfile the myfile to set */ public void setMyfile(FormFile myfile) { this.myfile = myfile; } }
3 、 Action 中调用 FormFile 中的数据,采用流输出, UploadTestAction.java :
package cn.huan.struts; import java.io.FileOutputStream; 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.action.DynaActionForm; import org.apache.struts.upload.FormFile; /** * 文件上传Action * @author 刘银环 * */ public class UploadTestAction extends Action { /* (non-Javadoc) * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadActionForm uaf = (UploadActionForm) form; String title = uaf.getTitle(); FormFile myFile = uaf.getMyfile(); FileOutputStream fos = new FileOutputStream("G:\\" + myFile.getFileName()); fos.write(myFile.getFileData()); fos.flush(); fos.close(); return mapping.findForward("success"); } }
4 、将 ActionForm 和 Action 配到 struts-config.xml 文件中:
<struts-config> <form-beans> <form-bean name="uploadForm" type="cn.huan.struts.UploadActionForm"/> </form-beans> <action-mappings> <action path="/upload" type="cn.huan.struts.UploadTestAction" name="uploadForm" scope="request" > <forward name="success" path="/upload_success.jsp"/> </action> </action-mappings> </struts-config>
5 、还可在 struts-config.xml 文件中配置上传的参数,如设置上传的文件大小最大为 50M :
<controller maxFileSize="50M"/>
到此, struts 文件上传工程建立完毕。
另:上面的 ActionForm 当然也可以采用 DynaActionForm 来完成,从而不用建 ActionForm 类, UploadTestAction 中相应代码改为:
DynaActionForm daf = (DynaActionForm) form; String title = daf.getString("title"); FormFile myFile = (FormFile) daf.get("myfile"); System.out.println("Title:"+title); FileOutputStream fos = new FileOutputStream("G:\\" + myFile.getFileName()); fos.write(myFile.getFileData()); fos.flush(); fos.close(); return mapping.findForward("success");
struts-config.xml 文件中关于 ActionForm 和 Action 的配置相应改为:
<struts-config> <form-beans> <!-- DynaActionForm for the upload --> <form-bean name="dynaForm2" type="org.apache.struts.action.DynaActionForm"> <form-property name="title" type="java.lang.String"/> <form-property name="myfile" type="org.apache.struts.upload.FormFile"/> </form-bean> </form-beans> <action-mappings> <action path="/upload" type="cn.huan.struts.UploadTestAction" name="dynaForm2" scope="request" > <forward name="success" path="/upload_success.jsp"/> </action> </action-mappings> <controller maxFileSize="50M"/> </struts-config>
。。。。。。。。。