posts - 68, comments - 19, trackbacks - 0, articles - 1

          導航

          <2012年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          26272829123
          45678910

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          評論排行榜

          Struts2的上傳

          Posted on 2012-02-28 11:24 viery 閱讀(255) 評論(0)  編輯  收藏 所屬分類: Struts2
          Struts2自帶上傳下載的實現:
          1.編寫輸入頁面和結果頁面
          input.jsp
          <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
          <%@ taglib prefix="s"  uri="/struts-tags"%>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
          <base href="<%=basePath%>">
              
              
          <title>My JSP 'index.jsp' starting page</title>
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->
              
              
          <script language="JavaScript">
              
          function addComponent()
              
          {
                  
          var td=document.getElementById("files");
                  
          var br=document.createElement("<br>");
                  
          var input=document.createElement("input");
                  
          var button=document.createElement("input");

                  input.name
          ="uploads";
                  input.type
          ="file";

                  button.type
          ="button";
                  button.value
          ="刪除";
                  button.onclick
          =function(){
                      td.removeChild(br);
                      td.removeChild(input);
                      td.removeChild(button);
                  }

                  td.appendChild(br);
                  td.appendChild(input);
                  td.appendChild(button);

                
              }



              
          </script>

              
            
          </head>
            
            
          <body>
               
          <input type="button" onclick="addComponent();" value="添加文件" />
                  
          <br />
                  
          <form onsubmit="return true;" action="upload"
                      method
          ="post" enctype="multipart/form-data">
                      
          <table>
                      
          <tr>
                      
          <td id="files">
                    
          <input type='file' name='uploads' />
                      
          </td>
                     
          </tr>
                     
          </table>
                 
                      
          <input type="submit" value="上傳" />
                  
          </form>
                
          <s:fielderror/>
            
          </body>
          </html>
          js用于自動生成和控制刪除上傳控件的數量。
          result.jsp
           
            <body>
              This a struts page. <br>
            </body>

          2.配置struts.xml


          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
          >
          <struts>
              
          <constant name="struts.custom.i18n.resources" value="message"></constant>
              
          <constant name="struts.i18n.encoding" value="gbk"/>
              
          <constant name="struts.multipart.saveDir" value="f:\"/>
              
          <constant name="struts.multipart.maxSize" value="2097152000"/>
              
          <package name="JsUpload" extends="struts-default">
                  
          <action name="upload" class="org.vle.action.UploadAction">
                      
          <result>/result.jsp</result>
                      
          <result name="input">/upload.jsp</result>
                      
          <interceptor-ref name="fileUpload">
                          
          <param name="maximumSize">10240000</param>
                          
          <param name="allowedTypes">application/vnd.ms-powerpoint</param>
                      
          </interceptor-ref>
                      
          <interceptor-ref name="defaultStack"></interceptor-ref>
                  
          </action>
              
          </package>

          </struts>

          3實現action



          package org.vle.action;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.Iterator;
          import java.util.List;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.Action;
          import com.opensymphony.xwork2.ActionSupport;

          public class UploadAction extends ActionSupport {
              
              
          private List<File> uploads;
              
          private List<String> uploadsFileName;
              
          private List<String>  uploadsContentType;

              

              
          public List<String> getUploadsContentType() {
                  
          return uploadsContentType;
              }


              
          public void setUploadsContentType(List<String> uploadsContentType) {
                  
          this.uploadsContentType = uploadsContentType;
              }


              
          public List<File> getUploads() {
                  
          return uploads;
              }


              
          public void setUploads(List<File> uploads) {
                  
          this.uploads = uploads;
              }


              



              
          public List<String> getUploadsFileName() {
                  
          return uploadsFileName;
              }


              
          public void setUploadsFileName(List<String> uploadsFileName) {
                  
          this.uploadsFileName = uploadsFileName;
              }




              @Override
              
          public String execute() throws Exception {
                  
                      
          for(int i=0;i<uploads.size();i++){
                          InputStream is
          =new FileInputStream(uploads.get(i));
                          String name
          =uploadsFileName.get(i);
                          String type
          =uploadsContentType.get(i);
                          System.out.println(type);
                          String path
          =ServletActionContext.getRequest().getRealPath("/temp");
                          File f
          =new File(path,name);
                          File temp
          =new File(path);
                          
          if(!temp.exists()){
                              temp.mkdirs();
                          }

                          OutputStream os
          =new FileOutputStream(f);
                          
          byte[] b=new byte[1024];
                          
          int len;
                          
          while((len=is.read(b))>0){
                              os.write(b, 
          0, len);
                          }

                          os.close();
                          is.close();
                      }

                      
              
                  
                  
                  
          return Action.SUCCESS;
              }

          }




          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 丽江市| 太保市| 府谷县| 滁州市| 宁夏| 建水县| 孟津县| 同德县| 河西区| 鹤峰县| 祁连县| 包头市| 鄂托克前旗| 报价| 东乌| 梁平县| 鄯善县| 东莞市| 鄱阳县| 河北区| 平武县| 鄢陵县| 赣州市| 大厂| 建德市| 卓尼县| 灌云县| 湾仔区| 宝应县| 凤山县| 宁明县| 新平| 太仓市| 应城市| 北碚区| 湘潭县| 巫山县| 河池市| 汉川市| 利辛县| 浮山县|