想飛就別怕摔

          大爺的并TM罵人

          struts文件上傳(簡單實例)

          struts文件上傳主要的文件有:
              2個jsp文件uploadIndex.jsp和uploadSuccess.jsp文件;FileUploadAction.java;FileUploadActionForm.java;struts-config.xml;web.xml;
              
          1、uploadIndex.jsp 注意:form中method="post" ;enctype="multipart/form-data"
          <%@ page language="java" pageEncoding="gb2312"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
          <title>測試Strust文件上傳</title>
            
          </head>
            
          <body>
                
          <form action="fileupload.do" method="post" enctype="multipart/form-data" >
                    
          <table>
                        標題:
          <input type="text" name="title"/>
                        
          <input type="file" name="myfile"/>
                        
          <input type="submit" value="上傳"/>
                    
          </table>
              
          </form>    
            
          </body>
          </html>
          2、uploadSuccess.jsp
           1 <%@ page language="java" pageEncoding="gb2312"%>
           2 <%@ page isELIgnored="false" %>
           3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
           4 <html>
           5   <head>
           6     <title>uploadSuccess.jsp</title>
           7   </head>
           8   <body>
           9     文件標題:${fileupload.title }<br>
          10     文件名:${fileupload.myfile.fileName }<br>
          11     文件大小:${fileupload.myfile.fileSize}<br>
          12   </body>
          13 </html>
          14 
          3、FileUploadActionForm.java
           1 package com.eplugger.struts.form;
           2 import org.apache.struts.action.ActionForm;
           3 import org.apache.struts.upload.FormFile;
           4 public class FileUploadActionForm extends ActionForm {
           5     private String title;
           6     private FormFile myfile;  //注意:上傳文件的類型必須是FormFile;它是struts提供的;
           7     public String getTitle() {
           8         return title;
           9     }
          10     public void setTitle(String title) {
          11         this.title = title;
          12     }
          13     public FormFile getMyfile() {
          14         return myfile;
          15     }
          16     public void setMyfile(FormFile myfile) {
          17         this.myfile = myfile;
          18     }
          19 }
          20 
          4、FileUploadAction.java
          package com.eplugger.struts.action;

          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.upload.FormFile;
          import com.eplugger.struts.form.FileUploadActionForm;

          public class FileUploadAction extends Action {
              @Override
              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
                      
          throws Exception {
                  
          //獲得form
                  FileUploadActionForm fpaf =(FileUploadActionForm)form;
                  
          //獲得通過form傳來的title值;
                  String title = fpaf.getTitle();
                  
          //獲得通過form傳來的文件;注意類型必須是FormFile;
                  FormFile myFile = fpaf.getMyfile();
                  
          if(myFile != null ){
                      
          //創建文件輸出的路徑
                      FileOutputStream fos= new  FileOutputStream("e:\\"+myFile.getFileName());
                      
          //輸出(一個bayt[]數組)
                      fos.write(myFile.getFileData());
                      
          //把內存中的文件變成物理的
                      fos.flush();
                      
          //關閉
                      fos.close();
                  }
                  request.setAttribute(
          "title","title");
                  
          return mapping.findForward("success");
              }
          }
          5、struts-config.xml
          <?xml version="1.0" encoding="gb2312"?>
          <!DOCTYPE struts-config PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
                  "http://struts.apache.org/dtds/struts-config_1_3.dtd"
          >
          <struts-config>
              
          <form-beans>
                  
          <form-bean name="fileupload" type="com.eplugger.struts.form.FileUploadActionForm"/>
              
          </form-beans>
              
          <action-mappings>
                  
          <action path="/fileupload"
                          type
          ="com.eplugger.struts.action.FileUploadAction"
                          name
          ="fileupload"
                          scope
          ="request"
                  
          >
                  
          <forward name="success" path="/uploadSuccess.jsp"/>
                  
          </action>
              
          </action-mappings>
              <controller maxFileSize="10M"/>

          </struts-config>
          6.web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" 
              xmlns
          ="http://java.sun.com/xml/ns/javaee" 
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          >
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>
            
            
          <servlet>
                  
          <servlet-name>action</servlet-name>
                  
          <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
                    
          <!-- Default -->
                  
          <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>2</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>detail</param-name>
                      
          <param-value>2</param-value>
                  
          </init-param>
                  
          <load-on-startup>2</load-on-startup>
              
          </servlet>

              
          <servlet-mapping>
                  
          <servlet-name>action</servlet-name>
                  
          <url-pattern>*.do</url-pattern>
              
          </servlet-mapping>
          </web-app>

          最后說一下,關于文件的大小等參數都可以再struts-config.xml文件中的<controller  />中配置,可以配置的屬性可以參考struts-core-1.3.10.jar中的org.apache.struts.resources.struts-config_1_3.dtd
          主要內容如下:

          <!-- The "controller" element describes the ControllerConfig bean
               [org.apache.struts.config.ControllerConfig] that encapsulates
               a module's runtime configuration. The following
               attributes are defined:

               bufferSize      The size of the input buffer used when processing
                               file uploads.
                               [4096]

               catalog         Name of the catalog to use when processing requests
                               for this module.
                               [struts]

               className       Fully qualified Java class name of the
                               ControllerConfig subclass for this controller object.
                               If specified, the object must be a subclass of the
                               default class.
                               ["org.apache.struts.config.ControllerConfig"]

               command         Name of the command to execute to process a request.
                               [servlet-standard]

               contentType     Default content type (and optional character encoding) to
                               be set on each response. May be overridden by the Action,
                               JSP, or other resource to which the request is forwarded.
                               ["text/html"]

               forwardPattern  Replacement pattern defining how the "path" attribute of a
                               <forward> element is mapped to a context-relative URL when
                               it starts with a slash (and when the contextRelative
                               property is false). This value may consist of any
                               combination of the following:
                               - "$M" - Replaced by the module prefix of this module
                               - "$P" - Replaced by the "path" attribute of the  selected
                               "forward" element
                               - "$$" - Causes a literal dollar sign to be rendered
                               - "$x" - (Where "x" is any character not defined above)
                               Silently swallowed, reserved for future use
                               If not specified, the default forwardPattern is "$M$P",
                               which is consistent with the previous behavior of
                               forwards.  Since Struts 1.1.  ["$M$P"]

               inputForward    Set to "true" if you want the "input" attribute of
                               <action> elements to be the name of a local or global
                               ActionForward, which will then be used to calculate the
                               ultimate URL. Set to "false" (the default) to treat the
                               "input" parameter of <action> elements as a
                               module-relative path to the resource
                               to be used as the input form. Since Struts 1.1.
                               [false]

               locale          Set to "true" if you want a Locale object stored in the
                               user's session if not already present.
                               [true]

               maxFileSize     The maximum size (in bytes) of a file to be accepted as a
                               file upload.  Can be expressed as a number followed by a
                               "K", "M", or "G", which are interpreted to mean kilobytes,
                               megabytes, or gigabytes, respectively.
                               ["250M"]

               memFileSize     The maximum size (in bytes) of a file whose contents will
                               be retained in memory after uploading. Files larger than
                               this threshold will be written to some alternative storage
                               medium, typically a hard disk. Can be expressed as a number
                               followed by a "K", "M", or "G", which are interpreted to
                               mean kilobytes, megabytes, or gigabytes, respectively.
                               ["256K"]

               multipartClass  The fully qualified Java class name of the multipart
                               request handler class to be used with this module.
                               ["org.apache.struts.upload.CommonsMultipartRequestHandler"]

               nocache         Set to "true" if you want the controller to add HTTP
                               headers for defeating caching to every response from
                               this module.  [false]

               pagePattern     Replacement pattern defining how the "page" attribute of
                               custom tags using it is mapped to a context-relative URL
                               of the corresponding resource.  This value may consist of
                               any combination of the following:
                               - "$M" - Replaced by the module prefix of this module
                               - "$P" - Replaced by the value of the "page" attribute
                               - "$$" - Causes a literal dollar sign to be rendered
                               - "$x" - (Where "x" is any character not defined above)
                                        Silently swallowed, reserved for future use
                               If not specified, the default forwardPattern is
                               "$M$P", which is consistent with previous hard coded
                               behavior of URL evaluation for "page" attributes.
                               ["$M$P"]

               processorClass  The fully qualified Java class name of the
                               RequestProcessor subclass to be used with this module.
                               ["org.apache.struts.chain.ComposableRequestProcessor"]

               tempDir         Temporary working directory to use when processing
                               file uploads.
                               [{Directory provided by servlet container}]

          -->

          posted on 2009-06-10 21:57 生命的綻放 閱讀(579) 評論(0)  編輯  收藏 所屬分類: Struts1.x

          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 枣强县| 绥德县| 锡林浩特市| 旺苍县| 西乌珠穆沁旗| 会东县| 彭州市| 穆棱市| 伊川县| 聂拉木县| 越西县| 阳城县| 邓州市| 墨脱县| 安国市| 马鞍山市| 浪卡子县| 青州市| 垦利县| 枣庄市| 高唐县| 孟津县| 武隆县| 随州市| 大理市| 翁源县| 铜鼓县| 石楼县| 天柱县| 扶风县| 梁平县| 新民市| 建水县| 南城县| 尉犁县| 武义县| 大宁县| 霍山县| 师宗县| 孟连| 抚松县|