1. 程式人生 > >Struts2 實現檔案上傳下載

Struts2 實現檔案上傳下載

檔案上傳對Struts2來說是一件非常容易的事情。Struts2檔案上傳依賴如下jar包

commons-fileupload-x.x.x.jar
commons-io-x.x.x.jar

檔案上傳大致步驟:

  1. 編寫頁面

  2. 配置struts.xml 攔截器會自動接收上傳的檔案

  3. 在Action中寫程式碼,把上傳的檔案儲存到伺服器中

  4. 響應客戶端

==注意== 檔案上傳form表單的enctype="multipart/form-data" 值需要修改

1. 單檔案上傳

==JSP頁面==

<form action="" method="post" enctype="multipart/form-data"> 
    <input type="file" value="選擇檔案" name="file" /><br>
    <input type="submit" value="提交" />
</form>

==Action編寫== Action 類需要注意一下幾點:

  • 要宣告與頁面中表單==name屬==性同名的屬性,同名的屬性的型別是==File型別==,該屬性用來儲存臨時檔案。

  • 要宣告==ContentType==屬性,型別是==String型別==,該屬性表示上傳檔案的型別。

  • 要宣告==FileName==屬性,型別是==String型別==,該檔案表示要上傳的檔案;

  • 給所有屬性提供get和set方法。

Action 類檔案上傳步驟:

  • 獲取要儲存檔案的位置;

  • 在目標資料夾內,建立一個與上傳檔案同名的檔案;

  • 通過FileUtils工具類提供copyFile()方法,將臨時檔案內容拷貝到目標資料夾下的那個同名的檔案;

  • 呼叫儲存臨時檔案的屬性的delete()方法刪除臨時檔案(也就是File型別屬性的delete方法)。

private File file;
private String fileContentType;
private String fileFileName;
​
public String fileUpload() throws IOException {
    System.out.println(fileContentType + "---" + fileFileName);
​
    ServletContext sc = ServletActionContext.getServletContext();
    // 獲取要儲存檔案的位置
    String path = sc.getRealPath("/upload");
    // 建立一個與上傳檔案同名的檔案
    File uploadFile = new File(path, fileFileName);
    // 將臨時檔案內容拷貝到目標資料夾下的那個同名的檔案
    FileUtils.copyFile(file, uploadFile);
    // 刪除臨時檔案
    file.delete();
​
    return "success";
}
//省略get/set方法

==注意==:Struts2預設上傳檔案總大小為2MB,超過檔案總大小。需要修改大小如下:

<!-- 單位是位元組 -->
<constant name="struts.multipart.maxSize" value="500000000" />

在Action下配置攔截器實現上傳檔案限制

<interceptor-ref name="fileUpload">
    <!--配置上傳檔案的大小,這裡配置的是上傳檔案的單個大小-->
    <param name="maximumSize">20971520</param>   
    <!-- 配置上傳檔案允許的型別-->
    <param name="allowedTypes">image/pjpeg,image/jpeg,text/plain,application/msword</param>
    <!-- 配置上傳檔案的副檔名-->
    <param name="allowedExtensions">.txt,.doc</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>

==經驗分享==

  1. 在 Action 判斷檔案大小後再限制上傳這是一種錯誤的做法。因為上傳是在攔截器中完成的。它在執行Action之前,到了Action中在限制上傳檔案已經遲了(快取檔案已經儲存到伺服器)。

  2. struts.multipart.maxSize 預設值是2M,如果上傳大於2M的檔案一定要修改其值

  3. 由於不同瀏覽器對上傳檔案型別的定義區別,實現檔案型別控制時需要考慮相容性。例如:JPG檔案需要在allowedTypes引數中定義image/pjpeg,image/jpeg兩種型別

  4. 配置攔截器時,不要忘記配置<interceptor-ref name="defaultStack"></interceptor-ref>

<br />

2. 多檔案上傳

多檔案上傳的所有流程與單檔案上傳一致,但是需要注意的是:

  • 在頁面中,雖然是多檔案上傳,但是頁面中表單的name屬性的值必須保持一致;

  • 在Action類中宣告的相關屬性,型別改成陣列或者集合。

  • 在業務方法中,相關處理流程改成單檔案上傳的迴圈。

==JSP頁面==

<form action="filesUploadAction" method="post" enctype="multipart/form-data">
    <input type="file" value="選擇檔案" name="file" /><br /><br />
    <input type="file" value="選擇檔案" name="file" /><br /><br />
    <input type="file" value="選擇檔案" name="file" /><br /><br />
    <input type="file" value="選擇檔案" name="file" /><br /><br />
    
     <input type="submit" value="提交" />
</form>

==Action類==

private File[] file;
private String[] fileContentType;
private String[] fileFileName;
​
public String fileUpload() throws IOException {
    ServletContext sc = ServletActionContext.getServletContext();
    // 獲取要儲存檔案的位置
    String path = sc.getRealPath("/upload");
    for (int i = 0; i < file.length; i++) {
        // 建立一個與上傳檔案同名的檔案
        File uploadFile = new File(path, fileFileName[i]);
        // 將臨時檔案內容拷貝到目標資料夾下的那個同名的檔案
        FileUtils.copyFile(file[i], uploadFile);
        // 刪除臨時檔案
        file[i].delete();
    }
    return "success";
}

3. 檔案下載

==JSP頁面==

<a href="downloadFileAction?downloadFileName=3dd87b1aec.jpg">下載圖片</a>

==struts.xml==

<!-- 檔案下載 -->
<action name="downloadFileAction" class="com.javaee.struts2.action.DownloadFileAction" method="downloadFile">
    <result name="success" type="stream">
        <!-- 指定流方法 -->
        <param name="inputName">inputString</param>
        <!-- 下載儲存的名字 -->
        <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
        <!--指定下載檔案的緩衝大小 -->
        <param name="bufferSize">4096</param>
        <!-- 設定下載檔案的型別 -->
        <param name="contentType">${contentType}</param>
    </result>
</action>

==Action類==

//下載的檔名
private String downloadFileName;
//下載檔案的儲存路徑
private String downPath;
//下載檔案的型別
String contentType;
​
public String downloadFile() throws UnsupportedEncodingException {
    contentType = "application/x-msdownload";
    downPath = "down" + File.separator + downloadFileName;
    return SUCCESS;
}
​
public InputStream getInputString() throws FileNotFoundException {
//      return ServletActionContext.getServletContext().getResourceAsStream(downPath);
    String path = ServletActionContext.getServletContext().getRealPath("/down");
    return new FileInputStream(new File(path,downloadFileName));
}
//省略get/set方法