struts2 下载文件
来源:互联网 发布:五轴编程工程师招聘 编辑:程序博客网 时间:2024/04/29 11:04
文件的下载,需要利用到struts2的Stream结果类型:
首先来看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会下载该文件.
- struts2实现文件下载
- struts2文件下载
- Struts2控制文件下载
- struts2文件下载
- Struts2 文件下载
- ext + struts2 下载文件
- Struts2文件下载
- Struts2文件下载
- struts2文件下载
- Struts2文件下载
- Struts2 的文件下载
- Struts2 的文件下载
- Struts2文件的下载
- Struts2文件下载
- struts2.1文件下载
- struts2文件下载
- struts2下载文件
- struts2 文件下载
- 粒子效果 CCParticleSystem 编码的实现
- java中传中文乱码解决
- vim
- C++中extern “C”含义深层探索
- Inside Microsoft SQL Server 2005: T-SQL Programming (数据类型|第一章) 字符串
- struts2 下载文件
- 水晶报表---行计算与列计算和每页统计计算
- linux下bluetooth编程(一)基础概念
- Android开发无法打开模拟器的问题:use '@foo' to launch a virtual device named
- jsp页面之间,数据库·······乱码问题
- 云计算Hadoop配置(二) ——单机伪分布配置
- Daizi's path system
- linux下bluetooth编程(二)blueZ协议栈
- uva10065-