初识Struts(七)----Struts文件上传

<!-- /* 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;
	}

}

 

3Action 中调用 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 、将 ActionFormAction 配到 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 文件中关于 ActionFormAction 的配置相应改为:

<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>

 。。。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值