我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          Struts1.2實(shí)現(xiàn)單文件上傳

          jsp:
          <%@ page language="java" pageEncoding="GBK"%>
          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
           
          <html> 
              
          <head>
                  
          <title>JSP for UpfileForm form</title>
              
          </head>
              
          <body>
                  
          <html:form action="/upfile" enctype="multipart/form-data">
                      file : 
          <html:file property="file" /><html:errors property="file"/><br/>
                      
          <html:submit value="確定"/>
                  
          </html:form>
              
          </body>
          </html>

          注意表單類型必須為:enctype="multipart/form-data".
          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="upfileForm" type="com.yourcompany.struts.form.UpfileForm" />

            
          </form-beans>

            
          <global-exceptions />
            
          <global-forwards />
            
          <action-mappings >
              
          <action
                
          attribute="upfileForm"
                input
          ="/upfile.jsp"
                name
          ="upfileForm"
                path
          ="/upfile"
                scope
          ="request"
                type
          ="com.yourcompany.struts.action.UpfileAction" />

            
          </action-mappings>

            
          <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
          </struts-config>


          web.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            
          <servlet>
              
          <servlet-name>action</servlet-name>
              
          <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
              
          <init-param>
                
          <param-name>config</param-name>
                
          <param-value>/WEB-INF/struts-config.xml</param-value>
              
          </init-param>
              
          <init-param>
                
          <param-name>debug</param-name>
                
          <param-value>3</param-value>
              
          </init-param>
              
          <init-param>
                
          <param-name>detail</param-name>
                
          <param-value>3</param-value>
              
          </init-param>
              
          <load-on-startup>0</load-on-startup>
            
          </servlet>
            
          <servlet-mapping>
              
          <servlet-name>action</servlet-name>
              
          <url-pattern>*.do</url-pattern>
            
          </servlet-mapping>
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>
          </web-app>

          UpfileForm.java:
          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           
          */

          package com.yourcompany.struts.form;

          import javax.servlet.http.HttpServletRequest;
          import org.apache.struts.action.ActionErrors;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.upload.FormFile;

          /** 
           * MyEclipse Struts
           * Creation date: 09-28-2007
           * 
           * XDoclet definition:
           * @struts.form name="upfileForm"
           
          */

          public class UpfileForm extends ActionForm {
              
          /*
               * Generated fields
               
          */


              
          /** file property */
              
          private FormFile file;


              
          /*
               * Generated Methods
               
          */


              
          /** 
               * Method validate
               * 
          @param mapping
               * 
          @param request
               * 
          @return ActionErrors
               
          */

              
          public ActionErrors validate(ActionMapping mapping,
                      HttpServletRequest request) 
          {
                  
          // TODO Auto-generated method stub
                  return null;
              }


              
          /** 
               * Method reset
               * 
          @param mapping
               * 
          @param request
               
          */

              
          public void reset(ActionMapping mapping, HttpServletRequest request) {
                  
          // TODO Auto-generated method stub
              }


              
          public FormFile getFile() {
                  
          return file;
              }


              
          public void setFile(FormFile file) {
                  
          this.file = file;
              }


              
          }

          UpfileAction.java:
          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           
          */

          package com.yourcompany.struts.action;

          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;

          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.upload.FormFile;

          import com.yourcompany.struts.form.UpfileForm;

          /** 
           * MyEclipse Struts
           * Creation date: 09-28-2007
           * 
           * XDoclet definition:
           * @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
           
          */

          public class UpfileAction extends Action {
              
          /*
               * Generated Methods
               
          */


              
          /** 
               * Method execute
               * 
          @param mapping
               * 
          @param form
               * 
          @param request
               * 
          @param response
               * 
          @return ActionForward
               
          */

              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          {
                  UpfileForm upfileForm 
          = (UpfileForm) form;
                  FormFile file 
          = upfileForm.getFile();
                  FileOutputStream fileOutput;
                  
                  
          try {
                      fileOutput 
          = new FileOutputStream("d://" + file.getFileName());
                      fileOutput.write(file.getFileData());
                      fileOutput.flush();
                      fileOutput.close();
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
                  

                  
          return null;
              }

          }
          當(dāng)然對于Form我們也可以用動態(tài)的:
          LazyValidatorForm uploadForm = (LazyValidatorForm) form;
          然后FormFile可以直接 通過form得到:
          FormFile formFile = uploadForm.get("file");

          相應(yīng)的在web.xml中的配置:
          <form-bean name="uploadForm" type="org.apache.struts.validator.LazyValidatorForm" />

          源碼下載


          posted on 2008-01-31 18:11 々上善若水々 閱讀(12087) 評論(3)  編輯  收藏 所屬分類: Struts1.x

          評論

          # re: Struts1.2實(shí)現(xiàn)單文件上傳  回復(fù)  更多評論   

          方法太牛比了。mail:yaoyao19851023@163.com
          2008-11-19 13:07 | yaoyao

          # re: Struts1.2實(shí)現(xiàn)單文件上傳  回復(fù)  更多評論   

          不靠譜,用接口里的方法如何得到文件流?
          FormFile file = upfileForm.getFile();
          FileOutputStream fileOutput;

          try {
          fileOutput = new FileOutputStream("d://" + file.getFileName());
          fileOutput.write(file.getFileData());
          2011-05-03 10:16 | Stan

          # re: Struts1.2實(shí)現(xiàn)單文件上傳[未登錄]  回復(fù)  更多評論   

          文件過大怎么么處理呀?謝啦
          2012-05-12 19:24 | 陳晨
          主站蜘蛛池模板: 龙里县| 德阳市| 邹平县| 新巴尔虎左旗| 星子县| 天气| 辽源市| 葵青区| 信丰县| 高雄县| 荆门市| 城步| 台北市| 庄河市| 张北县| 瑞丽市| 渝中区| 张家界市| 灵山县| 府谷县| 辰溪县| 蒙山县| 临安市| 龙井市| 沙湾县| 泌阳县| 德化县| 布尔津县| 吉木乃县| 龙游县| 蒲江县| 太湖县| 和政县| 新巴尔虎右旗| 穆棱市| 赤壁市| 昂仁县| 龙海市| 新宁县| 泽库县| 从江县|