<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    想飛就別怕摔

    大爺的并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 生命的綻放 閱讀(575) 評論(0)  編輯  收藏 所屬分類: Struts1.x

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

    導航

    統計

    常用鏈接

    留言簿(5)

    隨筆分類(94)

    隨筆檔案(93)

    文章分類(5)

    文章檔案(5)

    相冊

    JAVA之橋

    SQL之音

    兄弟之窗

    常用工具下載

    積分與排名

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 91大神亚洲影视在线| 最新欧洲大片免费在线 | 色噜噜AV亚洲色一区二区| 亚洲国产第一页www| 日韩a毛片免费观看| 成人人观看的免费毛片| 久久久久久亚洲精品成人| v片免费在线观看| 国产免费资源高清小视频在线观看| 久久国产亚洲高清观看| 三级毛片在线免费观看| 中文字幕中韩乱码亚洲大片| 国产成人综合亚洲| 国产精品免费_区二区三区观看| 亚洲大片在线观看| 久久一区二区免费播放| 国产专区一va亚洲v天堂| 亚洲AV无码国产剧情| 免费毛片在线视频| 亚洲午夜精品一区二区麻豆| 在线观看免费人成视频色| 亚洲精品在线电影| 亚洲中文无码永久免费| 久久狠狠爱亚洲综合影院| 大地资源免费更新在线播放| 亚洲一区二区三区免费在线观看| 波多野结衣中文字幕免费视频| JLZZJLZZ亚洲乱熟无码| yy一级毛片免费视频| 国产亚洲一区二区三区在线不卡| 一级毛片无遮挡免费全部| 亚洲一区二区中文| 大香人蕉免费视频75| 国产性生大片免费观看性 | 免费大片黄在线观看yw| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | 亚洲AV综合色区无码二区偷拍| 国产精品深夜福利免费观看 | 亚洲国产精品第一区二区三区| GOGOGO高清免费看韩国| 亚洲日韩国产精品乱-久|