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