struts2的多個檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   java   ar   strong   

            成功:                

上篇文章描述了單個檔案的上傳和配置,下面主要講解下不同的地方:

index.jsp

 <head>      <script type="text/javascript">        function check(){            var file = document.getElementsByName("file");                var filename = document.getElementsByName("fileName");            var filetype = document.getElementsByName("fileType");            for(var i =0;i<file.length;i++){                if(file[i].value !=""){                      var f = file[i].value ;                           var type = f.substr(f.lastIndexOf(".")).toLowerCase();                    var name = f.substr(f.lastIndexOf("\\"));                    filename[i].value = name;                    filetype[i].value = type;                                          }else{                      alert("請選擇檔案");                    return false;                      }                    }            return true;        }    </script>  </head>    <body>        <p>stuts 2 多檔案上傳執行個體 </p>    ${requestScope.typeError}          <form action="/uploadMore.do" method="post" enctype="multipart/form-data" onsubmit="return check();">       上傳檔案1:<input type="file" name="file" /><br>                <input type="hidden" name="fileType"/><br>                <input type="hidden" name="fileName"/><br>     上傳檔案2:<input type="file" name="file" /><br>            <input type="hidden" name="fileType"/><br>                <input type="hidden" name="fileName"/><br>           <input type="submit" value="提交"/>      </form>      </body>

struts.xml

 <package name="uploadMoreFile" extends="struts-default">          <!--多檔案上傳  -->             <action name="uploadMore" class="com.nn.upload.UploadMoreAction" method="uploadMore">                  <interceptor-ref name="fileUpload">                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>            </interceptor-ref>            <interceptor-ref name="defaultStack"/>                          <param name="savePath">/upload</param> <!-- 儲存的真實路徑 -->               <result name="success">/successMore.jsp</result>               <result name="input">/index.jsp</result>                                       </action>     </package>
UploadMoreAction.java
public class UploadMoreAction extends ActionSupport {    private File[] file;    private String[]  fileName;    private String[] fileType;     public String  uploadMore(){        String realpath = ServletActionContext.getServletContext().getRealPath("/upload");                if (this.file.length > 0) {            String[] name = this.getFileName();            for(int i =0;i<file.length;i++){                SimpleDateFormat  df = new SimpleDateFormat("yyyyMMddHHmmss");                          name[i] = df.format(new Date())+name[i].substring(1); //擷取檔案名稱,去掉\,並添加時間                File savefile = new File(new File(realpath), name[i]);                if (!savefile.getParentFile().exists())                    savefile.getParentFile().mkdirs();                try {                    FileUtils.copyFile(file[i], savefile);                } catch (IOException e) {                    e.printStackTrace();                }                ActionContext.getContext().put("message", "檔案上傳成功!");            }            //this.setFileName(name);         }        return "success";     }   
}

successMore.jsp

  <body>      ${requestScope.message}<br>    <s:iterator value="fileName" status="stuts">          檔案名稱:  <s:property value="fileName[#stuts.index]"/><br>              檔案為:   <img src="${pageContext.request.contextPath}/<s:property value="‘upload/‘+fileName[#stuts.index]"/>"><br>    </s:iterator>    <s:debug></s:debug>  </body>

 



struts2的多個檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.