Structs2實現文件上傳功能

Struts2自己並沒提供上傳的組件,咱們能夠經過調用上傳框架來實現文件的上傳。java

1、配置上傳解析器apache

首先要配置項目的框架,也就是倒導入"struts2-core-2.2.1.jar"庫文件,找到org.apache.struts2包下的default.porperties資源文件。以下圖;資源文件中給出了不一樣的strus2的默認配置,咱們可看到struts2默認是jakarta做爲其文件上傳的解析器。瀏覽器


jakarta是Commo-FileUpload的框架。若是要使用Commo-FileUpload框架來上傳文件,只需將"commons-fileupload-1.2.1.jar"和"commons-io-1.3.2.jar"兩個jar複製到項目中的WEB-INF/lib目錄下就可。緩存

若是想要使用COS框架來上傳文件,只需將「cos.jar」複製到項目中就能夠,而後在修改struts.multipart.parser常量值。app

修改常量值有兩種方法,一是在"struts.xml"中修改,代碼以下:框架

<constant name="struts.multipart.paeser" value="cos"></constant>jsp

struts.properties中修改,代碼以下:ide

sruts.multipart.parser=cospost

form的enctype屬性爲編碼方式,經常使用有兩種:application/x-www-form-
urlencoded和multipart/form-data,默認爲application/x-www-form-
urlencoded。
當action爲get時候,瀏覽器用x-www-form-urlencoded的編碼方式把form數據
轉換成一個字串(name1=value1&name2=value2...),而後把這個字串append到
url後面,用?分割,加載這個新的url。
當action爲post時候,瀏覽器把form數據封裝到http body中,而後發送到server。
若是沒有type=file的控件,用默認的application/x-www-form-urlencoded就能夠了。
可是若是有type=file的話,就要用到multipart/form-data了。瀏覽器會把整個表單以
控件爲單位分割,併爲每一個部分加上Content-Disposition(form-data或者
file),Content-Type(默認爲text/plain),name(控件name)等信息,並加上分割符
(boundary)。
 this

二.功能實現

upload.jsp

  
  
  
  
  1. <form action="upload.action" method="post" enctype="multipart/form-data"> 
  2.             <input type="file" name="upFile" /> 
  3.             <input type="submit" value="上傳" /> 
  4.         </form> 

UploadAction.java

 

  
  
  
  
  1. package com.travelsky.action;  
  2.  
  3. import java.io.*;  
  4.  
  5. import org.apache.struts2.ServletActionContext;  
  6.  
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.  
  9. public class UploadAction extends ActionSupport {  
  10.     private File upFile;          //獲取文件  
  11.     private String upFileFileName;// 獲取上傳文件名稱  
  12.     private String upFileContentType;// 獲取上傳文件類型  
  13.  
  14.     public File getUpFile() {  
  15.         return upFile;  
  16.     }  
  17.  
  18.     public void setUpFile(File upFile) {  
  19.         this.upFile = upFile;  
  20.     }  
  21.  
  22.     public String getUpFileFileName() {  
  23.         return upFileFileName;  
  24.     }  
  25.  
  26.     public void setUpFileFileName(String upFileFileName) {  
  27.         this.upFileFileName = upFileFileName;  
  28.     }  
  29.  
  30.     public String getUpFileContentType() {  
  31.         return upFileContentType;  
  32.     }  
  33.  
  34.     public void setUpFileContentType(String upFileContentType) {  
  35.         this.upFileContentType = upFileContentType;  
  36.     }  
  37.  
  38.     @Override 
  39.     public String execute() throws Exception {  
  40.         String path = ServletActionContext.getServletContext().getRealPath(  
  41.                 "/p_w_picpaths");  
  42.  
  43.         System.out.println(path);  
  44.         if (upFile != null) {  
  45.             File savefile = new File(new File(path), upFileFileName);  
  46.             if (!savefile.getParentFile().exists())  
  47.                 savefile.getParentFile().mkdirs();  
  48.             try {  
  49.                 InputStream is = new FileInputStream(upFile);    
  50.                 // 建立一個輸出流  
  51.                 OutputStream os = new FileOutputStream(savefile);  
  52.  
  53.                 // 設置緩存  
  54.                 byte[] buffer = new byte[1024];  
  55.  
  56.                 int length = 0;  
  57.  
  58.                 // 讀取myFile文件輸出到toFile文件中  
  59.                 while ((length = is.read(buffer)) > 0) {  
  60.                     os.write(buffer, 0, length);  
  61.                 }  
  62.                 // 關閉輸入流  
  63.                 is.close();  
  64.                 // 關閉輸出流  
  65.                 os.close();  
  66.  
  67.             } catch (IOException e) {  
  68.                 // TODO Auto-generated catch block  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }  
  72.  
  73.         return SUCCESS;  
  74.     }  
  75.  
  76. }  

structs.xml

 

  
  
  
  
  1. <struts> 
  2.     <constant name="struts.multipart.paeser" value="cos"></constant> 
  3. <action name="upload" class="com.travelsky.action.UploadAction"> 
  4.         <result name="success">upload.jsp</result> 
  5.         </action> 
  6.     </package> 
  7. </struts>
相關文章
相關標籤/搜索