[struts]文件上传

//test_upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@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:html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <html:form action="upload.do" enctype="multipart/form-data">
            please select you file to upload:
            <html:file property="file"/>
            <html:submit property="submit">
                </html:submit>
            </html:form>
    </head>
    <body>
     
    </body>
</html:html>

 

//struts-config.xml

  <form-bean name="bookForm" type="org.apache.struts.action.DynaActionForm">
         <form-property name="file" type="org.apache.struts.upload.FormFile"/>
     </form-bean>
    </form-beans>
   
    <global-exceptions>
   
    </global-exceptions>

    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
        <action path="/test1" type="cn.java.TestCheckboxAction" name="registForm" scope="request">
        </action>
        <action path="/upload" type="cn.java.TestUploadAction" name="bookForm" scope="request" validate="false">
            </action>
    </action-mappings>
   

 

 

//action类

public class TestUploadAction extends Action{
      public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
          HttpServletResponse response){
        
          DynaActionForm dyform=(DynaActionForm)form;
          FormFile theFile=(FormFile)dyform.get("file");
          if(!theFile.getFileName().equals("")){
          try{
           InputStream stream=theFile.getInputStream();
           String filePath="c://";
           OutputStream bos=new FileOutputStream(filePath+theFile.getFileName());
           int bytesRead=0;
           byte[] buffer=new byte[8192];
           while((bytesRead=stream.read(buffer,0, 8192))!=-1){
               bos.write(buffer, 0, bytesRead);
           }
           bos.close();
           stream.close();
          }catch(Exception e){
          e.printStackTrace();
          }
          }
          return null;
      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值