SSH簡單上傳功能

來源:互聯網
上載者:User

標籤:

Action:

  1 package com.smics.apps.smicwechat.action.upload;  2   3 import java.io.BufferedInputStream;  4 import java.io.BufferedOutputStream;  5 import java.io.File;  6 import java.io.FileInputStream;  7 import java.io.FileOutputStream;  8 import java.io.IOException;  9 import java.io.InputStream; 10 import java.io.OutputStream; 11  12 import org.springframework.util.StringUtils; 13  14 import com.opensymphony.webwork.ServletActionContext; 15 import com.smics.apps.smicwechat.action.BaseAction; 16 import com.smics.apps.smicwechat.domain.BasicInfo; 17  18 public class UploadPicture extends BaseAction { 19  20     private static final int BUFFER_SIZE = 16 * 1024; 21  22     private String direction;// 指向頁面類型 23  24     private File file;// 得到上傳的檔案 25     private String fileFileName;// 得到檔案的名稱 26     private String fileContentType;// 得到檔案的類型 27  28     private String savePath;// 儲存檔案的目錄 29     private boolean flag = false; 30  31     public String forward() { 32         if ("photo_gallery".equals(direction)) { 33             return direction; 34         } else if ("upload_picture".equals(direction)) { 35             return direction; 36         } 37         return ERROR; 38     } 39  40     @Override 41     public String execute() throws Exception { 42         // TODO Auto-generated method stub 43         // 根據伺服器的檔案儲存地址和源檔案名稱建立目錄檔案全路徑 44         String dstPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath()) + "\\" + this.getFileFileName(); 45         String path = this.getSavePath() + "\\" + this.getFileFileName(); 46         System.out.println("上傳的檔案的類型:" + this.getFileContentType()); 47         System.out.println("上傳的檔案的絕對路徑:" + dstPath); 48         System.out.println("上傳的檔案的相對路徑:" + path); 49         File dstFile = new File(dstPath); 50         copy(this.getFile(), dstFile); 51         this.setFlag(true); 52         return SUCCESS; 53     } 54  55     // 自己封裝的一個把源檔案對象複製成目標檔案對象 56     private static void copy(File src, File dst) { 57         InputStream in = null; 58         OutputStream out = null; 59         try { 60             in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); 61             out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); 62             byte[] buffer = new byte[BUFFER_SIZE]; 63             int len = 0; 64             while ((len = in.read(buffer)) > 0) { 65                 out.write(buffer, 0, len); 66             } 67         } catch (Exception e) { 68             // TODO Auto-generated catch block 69             e.printStackTrace(); 70         } finally { 71             if (null != in) { 72                 try { 73                     in.close(); 74                 } catch (IOException e) { 75                     // TODO Auto-generated catch block 76                     e.printStackTrace(); 77                 } 78             } 79             if (null != out) { 80                 try { 81                     out.close(); 82                 } catch (IOException e) { 83                     // TODO Auto-generated catch block 84                     e.printStackTrace(); 85                 } 86             } 87         } 88     } 89  90  91     public String getFileContentType() { 92         return fileContentType; 93     } 94  95     public String getFileFileName() { 96         return fileFileName; 97     } 98  99     public void setFileFileName(String fileFileName) {100         this.fileFileName = fileFileName;101     }102 103     public void setFileContentType(String fileContentType) {104         this.fileContentType = fileContentType;105     }106 107     public String getSavePath() {108         return savePath;109     }110 111     public void setSavePath(String savePath) {112         this.savePath = savePath;113     }114 115     public String getDirection() {116         return direction;117     }118 119     public void setDirection(String direction) {120         this.direction = direction;121     }122 123     public File getFile() {124         return file;125     }126 127     public void setFile(File file) {128         this.file = file;129     }130 131     public boolean isFlag() {132         return flag;133     }134 135     public void setFlag(boolean flag) {136         this.flag = flag;137     }138 139 }

 

前台頁面:

1 <input type="file" name="file">

 

SSH簡單上傳功能

聯繫我們

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