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

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

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

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://m.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://m.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

     

    Struts2文件上傳

    大家都知道struts2對(duì)文件上傳做了很好的封裝,使文件上傳不再那么恐怖。這里面struts2的文件上傳主要依賴的是org.apache.struts2.interceptor.FileUploadInterceptor這個(gè)攔截器

    關(guān)于這個(gè)攔截器我不想做過多的研究,這里主要討論的是該攔截器里面定義的幾個(gè)關(guān)于文件上傳的重要屬性。

    protected Long maximumSize;   //允許上傳單個(gè)文件的大小單位為字節(jié)

    protected String allowedTypes; //允許上傳的文件類型詳見tomcatweb.xml文件

    protected Set allowedTypesSet;

    //允許上傳的文件類型Set集合詳見tomcatweb.xml文件

    allowedTypesallowedTypesSet屬性如有多個(gè)值之間用逗號(hào)隔開

    以上的屬性主要配置在struts.xml中對(duì)應(yīng)文件上傳Action的攔截器中

    示列:

    <!-- 文件上傳Action -->

             <action name="Upload" class="UploadAction">

                 <result name="success">success.jsp</result>

                 <result name="input">upload.jsp</result>

                 <interceptor-ref name="fileUpload">

    //設(shè)置允許上傳單個(gè)文件的大小單位為字節(jié)

                     <param name="maximumSize">102400</param>

    //允許上傳的文件類型詳見tomcatweb.xml文件

                   <param name="allowedTypesSet">application/msword</param>

                 </interceptor-ref>

                 <interceptor-ref name="defaultStack"/>

             </action>

    注意:要使用文件上傳功能我們必須顯實(shí)的在對(duì)應(yīng)文件上傳的Action中指定<interceptor-ref name="fileUpload">這個(gè)攔截器及在該攔截器中配置我們需要的參數(shù),最后別忘了附加上<interceptor-ref name="defaultStack"/>這個(gè)默認(rèn)的攔截器,這樣才能真正發(fā)揮struts2的文件上傳功能。

    示列代碼:

    Action中的示列代碼

    publicclass FileUploadAction extends ActionSupport {

        private File file;

        private String fileContentType;

        private String fileFileName;

        private String memo;

        @Override

        public String execute() throws Exception {

            String path=ServletActionContext.getRequest().getRealPath("/upload");

            if(file==null)

            {

                this.addFieldError("file", "文件不能為空,請(qǐng)選擇");

                returnINPUT;

            }else

            {

                InputStream is=new FileInputStream(this.getFile());

                OutputStream os=new FileOutputStream(new File(path,this.getFileFileName()));

                byte[] buf=newbyte[1024];

                int length=0;

                while((length=is.read(buf))>0)

                {

                    os.write(buf, 0, length);

                }

                is.close();

                os.close();

            }

            returnSUCCESS;

        }

        public File getFile() {

            returnfile;

        }

        publicvoid setFile(File file) {

            this.file = file;

        }

        public String getFileContentType() {

            returnfileContentType;

        }

        public String getFileFileName() {

            returnfileFileName;

        }

        public String getMemo() {

            returnmemo;

        }

        publicvoid setFileContentType(String fileContentType) {

            this.fileContentType = fileContentType;

        }

        publicvoid setFileFileName(String fileFileName) {

            this.fileFileName = fileFileName;

        }

        publicvoid setMemo(String memo) {

            this.memo = memo;

        }

    }

    jsp中的示列代碼

    <s:form action="Upload" enctype="multipart/form-data" method="post">

        <s:file name="file"></s:file>

        <s:textfield name="memo"></s:textfield>

        <s:submit></s:submit>

    </s:form>

    struts.xml中的示列代碼

    <action name="Upload" class="UploadAction">

                 <result name="success">/success.jsp</result>

                 <result name="input">/upload.jsp</result>

                 <interceptor-ref name="fileUpload">

                     <param name="maximumSize">102400</param>

                     <param name="allowedTypes">application/msword</param>

                 </interceptor-ref>

                 <interceptor-ref name="defaultStack"/>

    </action>

    注意:

    a)       設(shè)置文件上傳屬性在Action中對(duì)應(yīng)的類型的java.io.File;

    b)       設(shè)置文件上傳表單的enctype="multipart/form-data" method="post"

    private File file;

    private String fileContentType;

    private String fileFileName;

    c)       紅色標(biāo)注的與文件上傳表單中文件上傳屬性的name一致

    d)       藍(lán)色的為固定寫法

    e)       對(duì)應(yīng)Action中攔截器的配置

    <interceptor-ref name="fileUpload">

                     <param name="maximumSize">102400</param>

                     <param name="allowedTypes">application/msword</param>

    </interceptor-ref>

    <interceptor-ref name="defaultStack"/>

    Struts2多文件上傳示例代碼:

    jsp上傳頁面代碼:

    <%@ page language="java" contentType="text/html; charset=gbk"

        pageEncoding="gbk"%>

    <%@ taglib prefix="s" uri="/struts-tags"%> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=gbk">

    <title>Insert title here</title>

    </head>

    <script type="text/javascript">

        function clickme()

        {

            var divs=document.getElementById('mydiv');

            var br=document.createElement('br');

            var input=document.createElement('input');

            input.type="file";

            input.name="file"

            var button=document.createElement('input');

            button.type="button";

            button.value="remove";

            divs.appendChild(br);

            divs.appendChild(input);

            divs.appendChild(button);

            button.onclick=function()

            {

                divs.removeChild(br);

                divs.removeChild(input);

                divs.removeChild(button);

            }

        }

    </script>

    <body>

    <h1>Struts2</h1>

    <s:fielderror name="file"/>

    <s:form action="Upload" enctype="multipart/form-data" method="post">

        <s:file name="file" theme="simple"></s:file>

        <input type="button" value="Add More" onclick="clickme()"/>

        <div id="mydiv">

        </div>

        <s:textfield name="memo"></s:textfield>

        <s:submit></s:submit>

    </s:form>

    </body>

    </html>

    Actio中代碼:用戶自己寫代碼來判斷文件類型及文件大小

    package com.struts2.action;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.InputStream;

    import java.io.OutputStream;

    import java.util.List;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    @SuppressWarnings("serial")

    public class FileUploadAction extends ActionSupport {

             private List<File> file;

             private List<String> fileContentType;

             private List<String> fileFileName;

             private String memo;

             @Override

             public String execute() throws Exception {

                       String path=ServletActionContext.getRequest().getRealPath("/upload");

                       if(file==null)

                       {

                                this.addFieldError("file", "文件不能為空,請(qǐng)選擇");

                                return INPUT;

                       }else

                       {

                                InputStream is = null;

                                OutputStream os = null;

                                File f=null;

                                long fileSize=0;

                                String ext="";

                                for (int i = 0; i < file.size(); i++) {

                                         ext=fileFileName.get(i).substring(fileFileName.get(i).lastIndexOf(".")+1);

                                         f=this.getFile().get(i);

                                         fileSize=f.length();

                                         System.out.println("fileSize:"+fileSize);

                                         System.out.println("ext:"+ext);

                                         if("exe".equals(ext)||"jar".equals(ext)||"bat".equals(ext)||"msi".equals(ext))

                                         {

                                                   this.addFieldError("file", "the file is not allowed");

                                                   return INPUT;

                                         }

                                        

                                         if(fileSize>102000)

                                         {

                                                   this.addFieldError("file", "the file is too large");

                                                   return INPUT;

                                         }

                                        

                                         is=new FileInputStream(f);

                                         os=new FileOutputStream(new File(path,this.getFileFileName().get(i)));

                                         byte[] buf=new byte[1024];

                                         int length=0;

                                         while((length=is.read(buf))>0)

                                         {

                                                   os.write(buf, 0, length);

                                          }

                                }

                                is.close();

                                os.close();

                       }

                       return SUCCESS;

             }

             public List<File> getFile() {

                       return file;

             }

             public void setFile(List<File> file) {

                       this.file = file;

             }

             public List<String> getFileContentType() {

                       return fileContentType;

             }

             public void setFileContentType(List<String> fileContentType) {

                       this.fileContentType = fileContentType;

             }

             public List<String> getFileFileName() {

                       return fileFileName;

             }

             public void setFileFileName(List<String> fileFileName) {

                       this.fileFileName = fileFileName;

             }

             public String getMemo() {

                       return memo;

             }

             public void setMemo(String memo) {

                       this.memo = memo;

             }

    }

    Struts.xml中配置代碼:

    <!-- 文件上傳Action -->

                   <action name="Upload" class=" com.struts2.action.FileUploadAction">

                <result name="success">/success.jsp</result>

                <result name="input">/upload.jsp</result>

                <interceptor-ref name="fileUpload">

                <!--

                   <param name="maximumSize">102400</param>

                   <param name="allowedTypes">application/msword</param>

                 -->

                </interceptor-ref>

                <interceptor-ref name="defaultStack"/>   

            </action>

    posted on 2009-10-22 09:35 雪山飛鵠 閱讀(9510) 評(píng)論(3)  編輯  收藏 所屬分類: struts2

    Feedback

    # re: Struts2文件上傳[未登錄] 2009-11-28 14:49 java
    太感謝了,看到這篇文章才解決了我的問題
    加上了<interceptor-ref name="defaultStack"/>就能得到文件了  回復(fù)  更多評(píng)論
      

    # re: Struts2文件上傳 2011-11-16 14:21 asdf
    asdf  回復(fù)  更多評(píng)論
      

    # re: Struts2文件上傳 2011-11-16 14:21 asdf
    asdfsadfas2222  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 精品免费国产一区二区| 老司机亚洲精品影院| 久久免费公开视频| 亚洲精品中文字幕无乱码麻豆| 国产又大又粗又硬又长免费| 中国毛片免费观看| 亚洲一卡2卡4卡5卡6卡在线99 | 国产A∨免费精品视频| 久久久亚洲裙底偷窥综合| 国产成人无码a区在线观看视频免费| 一个人看的www免费高清| 亚洲国产综合第一精品小说| 亚洲AV成人精品日韩一区18p| 日韩中文字幕免费视频| 国产综合成人亚洲区| 亚洲精品视频在线观看免费| 免费又黄又爽又猛的毛片| 2019中文字幕免费电影在线播放| 国产精品无码亚洲精品2021| 亚洲综合久久综合激情久久 | 国产亚洲精AA在线观看SEE| 毛片免费在线观看网站| 青青操视频在线免费观看| 亚洲中文无码永久免| 婷婷亚洲综合五月天小说| 全亚洲最新黄色特级网站 | 亚洲日本在线免费观看| aa级女人大片喷水视频免费| 亚洲愉拍一区二区三区| 亚洲国产精品久久久久婷婷软件| 亚洲精品A在线观看| 韩国18福利视频免费观看| 最近最好最新2019中文字幕免费 | 在线观看永久免费视频网站| 中文字幕天天躁日日躁狠狠躁免费| 免费无码午夜福利片| 亚洲人成欧美中文字幕| 亚洲成a人片在线观看中文!!!| 亚洲一区无码中文字幕| 亚洲国产精品一区二区三区久久| 日本免费一区二区三区最新|