struts2中文件上傳的實現

使用了apache的common-fileupload組件,同時還依賴與common-io包,使用之前,需要引入這兩個包。

common-upload組件是把文件先上傳到一個臨時目錄,然後需要自己手動在Action中處理,將文件改名,並且複製到指定的目錄。

首先,創建文件上傳頁面:

<% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %> 
<% @ taglib prefix = " s " uri = " /struts-tags " %> 

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html xmlns ="http://www.w3.org/1999/xhtml" > 
< head > 
    < title > Struts 2 File Upload </ title > 
</ head > 
< body > 
    < s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" > 
        < s:file name ="myFile" label ="Image File" /> 
        < s:textfield name ="caption" label ="Caption" />        
        < s:submit /> 
    </ s:form > 
</ body > 
</ html >

需要注意的就是from中的enctype ="multipart/form-data",還有要注意file標籤的name值,要和後臺Action中的名字一致,下面看一下Action代碼。

 package tutorial;

 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Date;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionSupport;

 public class FileUploadAction extends ActionSupport {
     private static final long serialVersionUID = 572146812454l ;
     private static final int BUFFER_SIZE = 16 * 1024 ;
    
     private File myFile;
     private String contentType;
     private String fileName;
     private String imageFileName;
     private String caption;
    
     public void setMyFileContentType(String contentType) {
         this .contentType = contentType;
    } 
    
     public void setMyFileFileName(String fileName) {
         this .fileName = fileName;
    } 
        
     public void setMyFile(File myFile) {
         this .myFile = myFile;
    } 
    
     public String getImageFileName() {
         return imageFileName;
    } 
    
     public String getCaption() {
         return caption;
    } 
 
     public void setCaption(String caption) {
         this .caption = caption;
    } 
    
     private static void copy(File src, File dst) {
         try {
            InputStream in = null ;
            OutputStream out = null ;
             try {                
                in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                 byte [] buffer = new byte [BUFFER_SIZE];
                 while (in.read(buffer) > 0 ) {
                    out.write(buffer);
                } 
            } finally {
                 if ( null != in) {
                    in.close();
                } 
                 if ( null != out) {
                    out.close();
                } 
            } 
        } catch (Exception e) {
            e.printStackTrace();
        } 
    } 
    
     private static String getExtention(String fileName) {
         int pos = fileName.lastIndexOf( " . " );
         return fileName.substring(pos);
    } 
 
    @Override
     public String execute()     {        
        imageFileName = new Date().getTime() + getExtention(fileName);
        File imageFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);
        copy(myFile, imageFile);
         return SUCCESS;
    } 
    
}

其中的FileName和contentType是common-upload組件會自己賦值的,只要寫上這兩個固定的名字就行了。特別特別需要注意的是這兩個方法的set方法,不要使用myeclipse自動生成set方法,而是setMyFileFileName,看到有什麼區別了吧,函數名中加了一個MyFile,這個是我們自定義的用於接收傳過來的文件的對象。如果把這裏寫錯了,就會報空指針錯,這裏一定要特別注意!

把文件上傳到了網站根目錄下的UploadImages下,這個文件夾需要自己手動創建,不然會提示沒有權限。

接下來是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 > 
    < package name ="fileUploadDemo" extends ="struts-default" > 
        < action name ="fileUpload" class ="tutorial.FileUploadAction" > 
            < interceptor-ref name ="fileUploadStack" /> 
            < result name ="success" > /ShowUpload.jsp </ result > 
        </ action > 
    </ package > 
</ struts >

注意其中的interceptor-ref屬性的配置。

web.xml沒什麼區別,就不貼出來了。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章