轉:ext+struts2實現上傳

來源:互聯網
上載者:User

upload.html
    <html> 
    <head>    
       <title>上傳檔案</title>    
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">      
        <link rel="stylesheet" type="text/css" href="/ext-2.1/resources/css/ext-all.css"/> 
       <script type="text/javascript" src="/ext-2.1/adapter/ext/ext-base.js"></script> 
       <script type="text/javascript" src="/ext-2.1/ext-all.js"></script> 
       <script type="text/javascript" src="/newExt/upload.js"></script>     
   </head> 
   <body> 
   <h1>上傳檔案</h1> 
</body> 
   </html> 

<html>

<head>

    <title>上傳檔案</title>

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

    <link rel="stylesheet" type="text/css" href="/ext-2.1/resources/css/ext-all.css"/>

    <script type="text/javascript" src="/ext-2.1/adapter/ext/ext-base.js"></script>

    <script type="text/javascript" src="/ext-2.1/ext-all.js"></script>

    <script type="text/javascript" src="/newExt/upload.js"></script>

</head>

<body>

<h1>上傳檔案</h1>

</body>

</html>

upload.js

   1. Ext.onReady(function() { 
   2.  
   3.     var form = new Ext.form.FormPanel({ 
   4.  
   5.         fileUpload: true,    
   6.  
   7.         baseCls: 'x-plain', 
   8.  
   9.         layout:'absolute', 
  10.  
  11.         url:'save-form.php', 
  12.  
  13.         defaultType: 'textfield', 
  14.  
  15.         frame:true,    
  16.  
  17.         url: '/member/upload.action',// 
  18.  
  19.  
  20.  
  21.         items: [{ 
  22.  
  23.            xtype: 'textfield',     
  24.  
  25.            fieldLabel: '檔案名稱',     
  26.  
  27.            name: 'upload',     
  28.  
  29.            inputType: 'file'//檔案類型     
  30.  
  31.         }], 
  32.  
  33.         buttons: [{     
  34.  
  35.             text: '上傳',     
  36.  
  37.             handler: function() {     
  38.  
  39.                 form.getForm().submit({     
  40.  
  41.                    success: function(form, action){     
  42.  
  43.                       Ext.Msg.alert('資訊', '檔案上傳成功!');     
  44.  
  45.                    },     
  46.  
  47.                    failure: function(){     
  48.  
  49.                       Ext.Msg.alert('錯誤', '檔案上傳失敗');     
  50.  
  51.                    }     
  52.  
  53.                 });     
  54.  
  55.             }     
  56.  
  57.         }]  
  58.  
  59.     }); 
  60.  
  61.  
  62.  
  63.     var window = new Ext.Window({ 
  64.  
  65.         title: 'Resize Me', 
  66.  
  67.         width: 500, 
  68.  
  69.         height:300, 
  70.  
  71.         minWidth: 300, 
  72.  
  73.         minHeight: 200, 
  74.  
  75.         layout: 'fit', 
  76.  
  77.         plain:true, 
  78.  
  79.         bodyStyle:'padding:5px;', 
  80.  
  81.         buttonAlign:'center', 
  82.  
  83.         items: form 
  84.  
  85.     }); 
  86.  
  87.  
  88.  
  89.     window.show(); 
  90.  
  91. }); 

Ext.onReady(function() {

    var form = new Ext.form.FormPanel({

    fileUpload: true,  

        baseCls: 'x-plain',

        layout:'absolute',

        url:'save-form.php',

        defaultType: 'textfield',

        frame:true,  

        url: '/member/upload.action',//

        items: [{

       xtype: 'textfield',   

       fieldLabel: '檔案名稱',   

       name: 'upload',   

       inputType: 'file'//檔案類型   

        }],

        buttons: [{   

        text: '上傳',   

        handler: function() {   

        form.getForm().submit({   

       success: function(form, action){   

          Ext.Msg.alert('資訊', '檔案上傳成功!');   

       },   

       failure: function(){   

          Ext.Msg.alert('錯誤', '檔案上傳失敗');   

       }   

   });   

      }   

}]

    });

    var window = new Ext.Window({

        title: 'Resize Me',

        width: 500,

        height:300,

        minWidth: 300,

        minHeight: 200,

        layout: 'fit',

        plain:true,

        bodyStyle:'padding:5px;',

        buttonAlign:'center',

        items: form

    });

    window.show();

});

PhotoAction.java
view plaincopy to clipboardprint?

   1. public class PhotoAction extends SessionAwareAction { 
   2.  
   3.  
   4.  
   5.     private static final long serialVersionUID = -3991411826094455715L; 
   6.  
   7.      
   8.  
   9.      
  10.  
  11.     private File[] upload; 
  12.  
  13.  
  14.  
  15.     private String[] uploadFileName; 
  16.  
  17.  
  18.  
  19.     private String[] uploadContentType; 
  20.  
  21.  
  22.  
  23.     private String[] dir; 
  24.  
  25.  
  26.  
  27.     private String[] targetFileName;  
  28.  
  29.      
  30.  
  31.     /**為上傳檔案自動分配檔案名稱,避免重複
  32. 
  33.      * @param fileName
  34. 
  35.      * @return
  36. 
  37.      */ 
  38.  
  39.     private String generateFileName(String fileName) {  
  40.  
  41.         // 獲得目前時間 
  42.  
  43.         DateFormat format = new SimpleDateFormat("yyMMddHHmmss");  
  44.  
  45.         // 轉換為字串 
  46.  
  47.         String formatDate = format.format(Calendar.getInstance().getTime());  
  48.  
  49.         // 隨機組建檔案編號 
  50.  
  51.         int random = new Random().nextInt(10000);  
  52.  
  53.         // 獲得檔案尾碼名稱 
  54.  
  55.         int position = fileName.lastIndexOf(".");  
  56.  
  57.         String extension = fileName.substring(position);  
  58.  
  59.         // 組成一個新的檔案名稱 
  60.  
  61.         return formatDate + random + extension;  
  62.  
  63.     } 
  64.  
  65.      
  66.  
  67.     public String upload() throws IOException { 
  68.  
  69.         // 獲得upload路徑的實際目錄 
  70.  
  71.         @SuppressWarnings("unused") 
  72.  
  73.         Member member = (Member)getSessionMap().get("member"); 
  74.  
  75.         String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum"));  
  76.  
  77.         //獲得實際目錄 
  78.  
  79.         String targetDirectory = "C://";  
  80.  
  81.         String[] mydir = new String[upload.length];  
  82.  
  83.         String[] tname = new String[upload.length];  
  84.  
  85.          
  86.  
  87.         for (int i = 0; i < upload.length; i++) {  
  88.  
  89.             // 產生儲存檔案的檔案名稱 
  90.  
  91.             tname[i] = generateFileName(uploadFileName[i]);  
  92.  
  93.             // 儲存檔案的路徑 
  94.  
  95.             mydir[i] = targetDirectory + "//" + tname[i];  
  96.  
  97.  
  98.  
  99.             // 建立一個目標檔案 
100.  
101.             File target = new File(targetDirectory, tname[i]);  
102.  
103.             // 將臨時檔案複製到目標檔案 
104.  
105.             try { 
106.  
107.                 FileUtils.copyFile(upload[i], target); 
108.  
109.             } catch (IOException e) { 
110.  
111.                 this.setMessage(e.getMessage()); 
112.  
113.                 e.printStackTrace(); 
114.  
115.             }  
116.  
117.              
118.  
119.         }  
120.  
121.          
122.  
123.         this.setMessage("上傳" + upload.length + "張照片成功"); 
124.  
125.         return SUCCESS; 
126.  
127.     } 
128.  
129.  
130.  
131.     public File[] getUpload() { 
132.  
133.         return upload; 
134.  
135.     } 
136.  
137.  
138.  
139.     public void setUpload(File[] upload) { 
140.  
141.         this.upload = upload; 
142.  
143.     } 
144.  
145.  
146.  
147.     public String[] getUploadFileName() { 
148.  
149.         return uploadFileName; 
150.  
151.     } 
152.  
153.  
154.  
155.     public void setUploadFileName(String[] uploadFileName) { 
156.  
157.         this.uploadFileName = uploadFileName; 
158.  
159.     } 
160.  
161.  
162.  
163.     public String[] getUploadContentType() { 
164.  
165.         return uploadContentType; 
166.  
167.     } 
168.  
169.  
170.  
171.     public void setUploadContentType(String[] uploadContentType) { 
172.  
173.         this.uploadContentType = uploadContentType; 
174.  
175.     } 
176.  
177.  
178.  
179.     public String[] getDir() { 
180.  
181.         return dir; 
182.  
183.     } 
184.  
185.  
186.  
187.     public void setDir(String[] dir) { 
188.  
189.         this.dir = dir; 
190.  
191.     } 
192.  
193.  
194.  
195.     public String[] getTargetFileName() { 
196.  
197.         return targetFileName; 
198.  
199.     } 
200.  
201.  
202.  
203.     public void setTargetFileName(String[] targetFileName) { 
204.  
205.         this.targetFileName = targetFileName; 
206.  
207.     } 
208.  
209.  
210.  
211. } 

public class PhotoAction extends SessionAwareAction {

private static final long serialVersionUID = -3991411826094455715L;

private File[] upload;

private String[] uploadFileName;

private String[] uploadContentType;

private String[] dir;

private String[] targetFileName;

    /**為上傳檔案自動分配檔案名稱,避免重複

     * @param fileName

     * @return

     */

    private String generateFileName(String fileName) {

        // 獲得目前時間

        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");

        // 轉換為字串

        String formatDate = format.format(Calendar.getInstance().getTime());

        // 隨機組建檔案編號

        int random = new Random().nextInt(10000);

        // 獲得檔案尾碼名稱

        int position = fileName.lastIndexOf(".");

        String extension = fileName.substring(position);

        // 組成一個新的檔案名稱

        return formatDate + random + extension;

    }

   

    public String upload() throws IOException {

        // 獲得upload路徑的實際目錄

@SuppressWarnings("unused")

Member member = (Member)getSessionMap().get("member");

        String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum"));

        //獲得實際目錄

        String targetDirectory = "C://";

        String[] mydir = new String[upload.length];

        String[] tname = new String[upload.length];

       

        for (int i = 0; i < upload.length; i++) {

            // 產生儲存檔案的檔案名稱

            tname[i] = generateFileName(uploadFileName[i]);

            // 儲存檔案的路徑

            mydir[i] = targetDirectory + "//" + tname[i];

            // 建立一個目標檔案

            File target = new File(targetDirectory, tname[i]);

            // 將臨時檔案複製到目標檔案

            try {

FileUtils.copyFile(upload[i], target);

} catch (IOException e) {

this.setMessage(e.getMessage());

e.printStackTrace();

}

        }

       

this.setMessage("上傳" + upload.length + "張照片成功");

return SUCCESS;

}

public File[] getUpload() {

return upload;

}

public void setUpload(File[] upload) {

this.upload = upload;

}

public String[] getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(String[] uploadFileName) {

this.uploadFileName = uploadFileName;

}

public String[] getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(String[] uploadContentType) {

this.uploadContentType = uploadContentType;

}

public String[] getDir() {

return dir;

}

public void setDir(String[] dir) {

this.dir = dir;

}

public String[] getTargetFileName() {

return targetFileName;

}

public void setTargetFileName(String[] targetFileName) {

this.targetFileName = targetFileName;

}

}

設定檔

<package name="com.hm.member.action" namespace="/member" extends="struts-default">

<action name="upload" class="photoAction" method="upload">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.