超简单的struts文件上传

6 篇文章 0 订阅

Struts文件上传

1.       页面配置

<form action="upload.do" method="post" enctype="multipart/form-data">

       标题:<input type="text" name="title"><br>

       文件:<input type="file" name="file"><br>

       <input type="submit" value="提交">

  </form>

 

2.    ActionForm中使用FormFile来接受上传文件,代码如下:

    public class UploadForm extends ActionForm { 

    private FormFile file;

    public FormFile getFile() {

       return file;

    }

    public void setFile(FormFile file) {

       this.file = file;

    }

}

 

3.    Action中使用FormFile来接收上传文件,代码如下:

public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       ActionForward af = null;

       UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub

       FormFile myFile = uploadForm.getFile();      

       if(myFile!=null){

           try {

              FileOutputStream fos = new FileOutputStream("c://"+myFile.getFileName());

              fos.write(myFile.getFileData());

              fos.flush();

              fos.close();

              af =  mapping.findForward("success");

           } catch (Exception e) {

              e.printStackTrace();

              af =  mapping.findForward("error");

           }

       }

       return af;

    }

4.       struts-config.xml中的配置

配置form:

<form-bean name="uploadForm" type="com.zsw.struts.form.UploadForm" />

配置Action:

<action

      attribute="uploadForm"

      name="uploadForm"

      path="/upload"

      scope="request"

      type="com.zsw.struts.action.UploadAction" >

      <forward name="success" path="/upload_success.jsp" />

      <forward name="error" path="/upload_error.jsp" />

</action>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值