Struts2框架和文件下载有关的类是org.apache.struts2.dispatcher.StreamResult,这个类的一些成员变量和下载有关,看源码
//下载文件的类型
protected String contentType = "text/plain";
//下载文件的长度
protected String contentLength;
//
protected String contentDisposition = "inline";
//下载文件的文本编码
protected String contentCharSet ;
//指定文件输入流的名称,通常是InputStream的子类
protected String inputName = "inputStream";
protected InputStream inputStream;
//文件缓存大小
protected int bufferSize = 1024;
//是否允许缓存
protected boolean allowCaching = true;
DownLoadAction.java
package com.struts2.download;
import java.io.IOException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport {
private int number;
private String fileName;
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getDownLoadFile() throws IOException {
if(1==number){
this.fileName="Dream.jpg";
//获取资源路径
return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg");
}
else if(2==number){
this.fileName="jd2chm源码生成chm格式文档.rar" ;
//解决乱码
this.fileName=new String(this.fileName.getBytes("gbk"), "iso-8859-1");
return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar");
}
return null;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
filedownload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'filedownload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>下载内容</h2><br/>
Dream.jsp <a href="${pageContext.request.contextPath}/DownLoadAction.action?number=1">点击下载</a><br/>
jd2chm源码生成chm格式文档.rar<a href="${pageContext.request.contextPath}/DownLoadAction.action?number=2">点击下载</a><br/>
</body>
</html>
</package>
<package name="filedownload" extends="struts-default">
<action name="DownLoadAction" class="com.struts2.download.DownLoadAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param><!-- 指定下载文件类型 -->
<param name="contentDisposition">attacthment;filename="${fileName}"</param><!-- filename="${fileName}"指定下载文件名 -->
<param name="inputName">DownLoadFile</param>
<param name="bufferSize">1024</param><!--指定下载文件缓存的大小 -->
</result>
</action>
</package>
注意 DownLoadFile中DownLoadFile是DownLoadAction.java的getDownLoadFile方法去掉get的名称,这是约定的。