struts2 下载文件

来源:互联网 发布:五轴编程工程师招聘 编辑:程序博客网 时间:2024/04/29 11:04

文件的下载,需要利用到struts2Stream结果类型:

首先来看Struts2的返回类型(来自官网).Stream是用来把一个输入流返回到浏览器,这个就是用来文件下载的

Chain Result

Used for Action Chaining

Dispatcher Result

Used for web resource integration, including JSP integration

FreeMarker Result

Used for FreeMarker integration

HttpHeader Result

Used to control special HTTP behaviors

Redirect Result

Used to redirect to another URL (web resource)

Redirect Action Result

Used to redirect to another action mapping

Stream Result

Used to stream an InputStream back to the browser (usually for file downloads)

Velocity Result

Used for Velocity integration

XSL Result

Used for XML/XSLT integration

PlainText Result

Used to display the raw content of a particular page (i.e jsp, HTML)

Tiles Result

Used to provide Tiles integration

 

 

Stream结果类的参数(来自官网):

  • contentType - the stream mime-type as sent to the web browser (default = text/plain).                             MIME类型(默认是 text / plain)。用来设置HTTP响应里的Content-Type标头
  • contentLength - the stream length in bytes (the browser displays a progress bar).

字节流的长度(浏览器显示一个进度栏)。用来设置HTTP响应里的Content-Length标头

  • contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".

设置标题为什么文件名(规定文件名=“document.pdf”) 用来设置HTTP响应里的Content-Disposition标头

  • inputName - the name of the InputStream property from the chained action (default = inputStream).

一个动作类属性的名字,此属性返回的InputStream对象会被发送到浏览器

  • bufferSize - the size of the buffer to copy from input to output (default = 1024).

缓冲区大小(默认1024,单位是字节)。通过InputStream对象读取数据,通过OutputStream对象向浏览器发送数据时的缓冲区的长度

  • allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)

 如果设置为false,它将设置标题语无缓存,防止客户端从缓存读取内容。 (默认true

  • contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

如果设置为一个字符串, charset为设置的字符串,如果没有设置,然后将头没有字符集的设置

 

e.g:

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>

    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

       <default-action-ref name="index" />

   

       <action name="ViewCss" class="com.download.action.DownLoad">

           <result type="stream" name="success">

              <!-- Stream返回类型的各项参数 -->    

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

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

              <param name="contentDisposition">filename="main.css"</param>

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

           </result>

       </action>

      

       <action name="DownloadCss" class="com.download.action.DownLoad">

           <result type="stream" name="success">

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

              <param name="contentType">application/octet-stream</param>

              <param name="contentDisposition">filename="main.css"</param>

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

           </result>

       </action>

    </package>

</struts>

 

 

注意:这两个action的区别就是它们的contentType被设置成不同的值,ViewCss是把文件内容发送到浏览器,DownloacCss是下载文件.

 

 

 

Download.java内容

实现了ServletContextAware接口

package com.download.action;

 

import java.io.InputStream;

 

import javax.servlet.ServletContext;

 

import org.apache.struts2.util.ServletContextAware;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class Download extends ActionSupport implements ServletContextAware{

   

    private String filePath;

    private ServletContext servletContext;

   

    public void setServletContext(ServletContext servletContext) {

        this.servletContext = servletContext;

    }

    public void setFilePath(String filePath) {

        this.filePath = filePath;

    }

    public InputStream getInputStream() throws Exception {

        return servletContext.getResourceAsStream(filePath);

    }

 

}

 

 

 

index.jsp内容

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

       <title>Download</title>

       <style type="text/css">@import url(css/main.css);</style>

    </head>

 

    <body>

       <div id="global" style="width: 200px">

           <s:url id="url1" action="ViewCss">

              <s:param name="filePath">css/main.css</s:param>

           </s:url>

           <s:a href="%{url1}">View CSS</s:a>

 

           <br />

           <s:url id="url2" action="DownloadCss">

              <s:param name="filePath">css/main.css</s:param>

           </s:url>

           <s:a href="%{url2}">Download CSS</s:a>

       </div>

    </body>

</html>

点击View Css会把文件内容显示出来,点击Download Css会下载该文件.

原创粉丝点击