1.struts.xml文件中的配置
<action name="download" class="com.koubei.DownloadAction">
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
contentType指定下载文件类型,application/octet-stream指所有类型
attachment;filename="${filename}" 下载文件的名字,attachment;代表弹出下载框
2.这里我接着上面上传成功后的页面完成下载,所以把上一篇的上传成功的Success.jsp稍作改动
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功
<br/>
<s:a href="download.action?uploadFileName=%{uploadFileName}">
<s:property value="uploadFileName" />
</s:a>点击下载文件
<br/>
</body>
</html>
3.下载的动作类
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
public class DownloadAction extends ActionSupport {
private String uploadFileName;
private InputStream inputStream;
public String getuploadFileName() {
return uploadFileName;
}
public void setuploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public InputStream getInputStream() throws Exception {
String realPath = "d:\\Aggressive\\" + uploadFileName;
File file = new File(realPath);
inputStream = new FileInputStream(file);
return inputStream;
}
public String execute() {
return SUCCESS;
}
}
这样就实现下载功能了