自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

struts2文件上傳的采用的三種方式解析

開發(fā) 后端
上傳就是將信息從個(gè)人計(jì)算機(jī)(本地計(jì)算機(jī))傳遞到中央計(jì)算機(jī)(遠(yuǎn)程計(jì)算機(jī))系統(tǒng)上,讓網(wǎng)絡(luò)上的人都能看到。將制作好的網(wǎng)頁、文字、圖片等發(fā)布到互聯(lián)網(wǎng)上去,以便讓其他人瀏覽、欣賞。這一過程稱為上傳。

文件上傳幾乎是每個(gè)項(xiàng)目實(shí)現(xiàn)的一個(gè)必須的模塊。

上傳就是將信息從個(gè)人計(jì)算機(jī)(本地計(jì)算機(jī))傳遞到中央計(jì)算機(jī)(遠(yuǎn)程計(jì)算機(jī))系統(tǒng)上,讓網(wǎng)絡(luò)上的人都能看到。將制作好的網(wǎng)頁、文字、圖片等發(fā)布到互聯(lián)網(wǎng)上去,以便讓其他人瀏覽、欣賞。這一過程稱為上傳。

JAVA實(shí)現(xiàn)文件上傳的幾個(gè)組件:

1 SmartUpload 用的最多的一個(gè)組件,已經(jīng)不再更新了,可以實(shí)現(xiàn)上傳和下載

2 FileUpload Apache實(shí)現(xiàn)的文件上傳組件,功能齊備

3 J2KUpload java2000實(shí)現(xiàn)的文件上傳組件,全部使用內(nèi)存,適合多個(gè)不超過10M的小文件

下面具體說說FileUpload Apache實(shí)現(xiàn)的文件上傳組件。

1、/** 按copy方式上傳 */

Java代碼

  1. public String uploadFile(){     
  2.     /**保存的具體路徑*/    
  3.     String savepath = getSavePath();     
  4.     /**根據(jù)保存的路徑創(chuàng)建file對象*/    
  5.     File file = new File(savepath);     
  6.     if(!file.exists()){     
  7.         /**創(chuàng)建此文件對象路徑*/    
  8.         file.mkdirs();     
  9.     }     
  10.     try {     
  11.         /**使用的是:org.apache.commons.io.FileUtils FileUtils*/    
  12.         FileUtils.copyFile(pic, new File(file,getPicFileName()));     
  13.     } catch (IOException e) {     
  14.         e.printStackTrace();     
  15.     }     
  16.     return SUCCESS;     
  17. }  

 

備注:

1、getSavePath()方法中,ServletActionContext().getServletContext().getRealPath

(savePath+"\\"+getPicFileName()); ,這個(gè)主要是一個(gè)文件的實(shí)際路徑

2、我個(gè)人認(rèn)為這種方式是簡單易用的。按copy方式上傳使用的是Apache公司的

org.apache.commons.io.FileUtils包里的FileUtils.java。

2、/** 按字節(jié)方式上傳 */

Java代碼

  1. public String uploadFile(){          
  2.     /** 文件的寫操作 */      
  3.     FileInputStream fis = null;      
  4.     FileOutputStream fos = null;     
  5.         /** 保存的路徑 */    
  6.     String savepath = getSavePath();     
  7.     /** 根據(jù)保存的路徑創(chuàng)建file對象 */    
  8.     File file = new File(savepath);     
  9.     /** file對象是否存在   */    
  10.     if (!file.exists()) {     
  11.         /** 創(chuàng)建此文件對象路徑  */    
  12.         file.mkdirs();     
  13.     }     
  14.     try {     
  15.         /** 創(chuàng)建輸入流 */    
  16.         fis = new FileInputStream(pic);     
  17.         /** 輸出流 更據(jù)文件的路徑+文件名稱創(chuàng)建文件對象 */    
  18.         fos = new FileOutputStream(file + "//" + getPicFileName());     
  19.         /** 讀取字節(jié)   */    
  20.         byte b[] = new byte[1024];     
  21.         int n = 0;     
  22.         /** 讀取操作   */    
  23.         while ((n = fis.read(b)) != -1) {     
  24.             /** 寫操作   */    
  25.             fos.write(b, 0, n);     
  26.         }     
  27.         /** 關(guān)閉操作  */    
  28.         if (fis != null) {     
  29.             fis.close();     
  30.         }     
  31.         if (fos != null) {     
  32.             fos.close();     
  33.         }     
  34.     } catch (Exception e) {     
  35.         e.printStackTrace();     
  36.     }     
  37.     return SUCCESS;     
  38. }   

 

3、/** 按字符方式上傳 即“三層管道” */

Java代碼

  1. public String uploadFile(){     
  2.     /** 文件的寫操作 */    
  3.     BufferedReader br =null;     
  4.     BufferedWriter bw = null;        
  5.     /** 保存的路徑 */    
  6.     String savepath = getSavePath();     
  7.     /** 根據(jù)保存的路徑創(chuàng)建file對象   */    
  8.     File file = new File(savepath);     
  9.     /** file對象是否存在  */    
  10.     if (!file.exists()) {                
  11.         /** 創(chuàng)建此文件對象路徑  */    
  12.         file.mkdirs();     
  13.     }     
  14.     try {     
  15.         /**   創(chuàng)建一個(gè)BufferedReader  對象*/    
  16.         br = new BufferedReader(new InputStreamReader(new FileInputStream     
  17.     
  18. (pic)));                 
  19.         bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream     
  20.     
  21. (file + "//" + getPicFileName())));     
  22.                  
  23.         // 讀取字節(jié)     
  24.         char b[] = new char[1024];     
  25.         int n = 0;     
  26.         // 讀取操作     
  27.         while ((n = br.read(b)) != -1) {     
  28.             // 寫操作     
  29.             bw.write(b, 0, n);     
  30.         }     
  31.         // 關(guān)閉操作     
  32.         if (br != null) {     
  33.             br.close();     
  34.         }     
  35.         if (bw != null) {     
  36.             bw.close();     
  37.         }     
  38.     } catch (Exception e) {     
  39.         e.printStackTrace();     
  40.     }     
  41.     return SUCCESS;     
  42. }   

 

備注:

第二種上傳方式?jīng)]有第三種上傳方式效率高。

建議:

***用***種方式上傳,次之使用第三種方式上傳,***再使用第二種方式上傳。

責(zé)任編輯:金賀 來源: 博客園
相關(guān)推薦

2009-06-04 09:41:50

struts2上傳文件

2009-06-25 15:50:03

Struts2教程上傳任意多個(gè)文件

2009-02-04 14:00:59

2010-09-13 12:19:03

2009-06-08 16:44:00

Struts2文件上傳

2020-10-26 14:03:07

混合云云計(jì)算云遷移

2009-06-25 15:26:25

Struts2教程struts.xml常

2012-07-17 09:16:16

SpringSSH

2009-06-04 08:34:24

Struts2配置struts.xml

2009-03-09 11:27:55

2022-08-10 10:24:30

人工智能疫情數(shù)據(jù)架構(gòu)

2014-12-31 17:42:47

LBSAndroid地圖

2021-06-24 08:52:19

單點(diǎn)登錄代碼前端

2021-11-05 21:33:28

Redis數(shù)據(jù)高并發(fā)

2019-11-20 18:52:24

物聯(lián)網(wǎng)智能照明智能恒溫器

2009-02-04 11:37:15

2009-07-29 09:54:34

struts2和str

2012-05-10 14:00:06

StrutsjsonJava

2020-11-01 17:10:46

異步事件開發(fā)前端

2009-06-04 09:20:19

struts2 if標(biāo)使用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號