Struts2(十四)---文件的上傳與下載

(一)文件的上傳
(1)表單的準備
~須把HTML表單的entype屬性設置爲multipart/form-data.
~須把HTML表單的method屬性設置爲post.
~須添加<input type="file">字段.
(2)Struts2對文件上傳的支持
· 在Struts應用程序裏,FileUpload攔截器和Commons FileUpload組件可以完成文件的上傳.
· 基本的文件上傳步驟:
①在jsp頁面的文件上傳表單裏使用file標籤,如果需要一次上傳多個文件,就必須使用多個file標籤,但它們的名字必須是相同的;

<s:form action="uploadListAction" enctype="multipart/form-data" method="post" theme="simple">
  <s:actionerror/>
  <s:fielderror name="ppt"></s:fielderror>
  File:<s:file label="File" name="ppt"></s:file>
  Desc:<s:textfield label="Desc" name="desc[0]"></s:textfield>
  <br>
  File1:<s:file label="File1" name="ppt" ></s:file>
  Desc:<s:textfield label="Desc" name="desc[1]" ></s:textfield>
  <br>
  File2:<s:file label="File2" name="ppt"></s:file>
  Desc:<s:textfield label="Desc" name="desc[2]" ></s:textfield>
  <br>
  <s:submit></s:submit>
</s:form>

②在Action中新添加3個和文件上傳相關的屬性,並提供對應的getter和setter方法.這3個屬性的名字必須是以下格式:

Action中屬性名稱 類型 描述 示例
[File Name] File 被上傳的文件 data
[File Name]ContentType String 上傳文件的文件名 dataContentType
[File Name]FileName String 上傳文件的文件名 dataFileName

注:若要上傳多個文件,則上述的三個屬性,可以改爲List類型!多個文件域的name屬性值需要一致.
(3)配置FileUpload攔截器
· maximumSize: 上傳單個文件的最大長度(以字節爲單位), 默認值爲 2 MB
· allowedTypes: 允許上傳文件的類型, 各類型之間以逗號分隔
· allowedExtensions: 允許上傳文件擴展名, 各擴展名之間以逗號分隔
可以在 struts.xml 文件中覆蓋這 3 個屬性

   <interceptors>
     <interceptor name="hello" class="com.ty.myInterceptor.MyInterecptor"></interceptor>
     <interceptor-stack name="tyStack">
        <interceptor-ref name="defaultStack">
            <param name="fileUpload.maximumSize">15360</param>
            <param name="fileUpload.allowedTypes">text/html</param>
            <param name="fileUpload.allowedExtensions">html</param>
        </interceptor-ref>
     </interceptor-stack>
     </interceptors>
     <default-interceptor-ref name="tyStack"></default-interceptor-ref>

· Commons FileUpload 組件默認接受上傳文件總的最大值爲 2M, 可以通過在 struts 配置文件中配置常量的方式修改.
· 與文件上傳有關的出錯消息在 struts-messages.properties 文件裏預定義. 可以在文件上傳 Action 相對應的資源文件中重新定義錯誤消息.

struts.messages.error.uploading  ---文件上傳出錯的消息.
struts.messgaes.error.file.too.large ---文件超過最大值的消息.
struts.messages.error.content.type.not.allowed ---文件內容類型不合法的消息
struts.messages.error.file.extension.allowed --- 文件擴展名不合法的消息

(二)文件的下載
(1)Struts2中使用type=”stream”的result進行下載即可.
(2)可以爲Stream的result設定如下參數
· contentType:被下載的文件的 MIME 類型。默認值爲 text/plain
· contentLength:被下載的文件的大小,以字節爲單位
· contentDisposition: 可以設置下載文件名的ContentDispositon 響應頭,默認值爲 inline,通常設置爲如下格式: attachment;filename=”document.pdf”.
· inputName:Action 中提供的文件的輸入流。默認值爲 inputStream
· bufferSize:文件下載時緩衝區的大小。默認值爲 1024
· allowCaching :文件下載時是否允許使用緩存。默認值爲 true
· contentCharSet:文件下載時的字符編碼。
例:

Action類:

    private static final long serialVersionUID = 1L;
    private String contentType;
    private long contentLength;
    private String contentDisposition;
    private InputStream inputStream;

    public String getContentType(){
        return contentType;
    }
    public long getContentLength(){
        return contentLength;
    }

    public String getContentDisposition(){
        return contentDisposition;
    }
    public InputStream getInputStream(){
        return inputStream;
    }

    public String execute() throws IOException{

        ServletContext servletContext = ServletActionContext.getServletContext();
        String fileName = servletContext.getRealPath("files/hidden.html");
        inputStream = new FileInputStream(fileName);
        contentLength = inputStream.available();
        return "success";
    }

}
Struts.xml 配置文件
-<action name="testDownload" class="com.ty.exer1.DownloadAction">


-<result type="stream">

<param name="contentType">text/html</param>

<param name="inputName">inputStream</param>

<param name="contentDisposition">attchment;fileName=hidden.html</param>

<param name="bufferSize">1024</param>

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