jsp代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="download.action?fileName=1.txt" mce_href="download.action?fileName=1.txt">文件</a> </body> </html> 文件下载action代码: package cn.struts; import java.io.File; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileDownload extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String fileName; private InputStream inputStream; private String contentType; static Map<String,String> contentTypes = new HashMap<String,String>(); static { contentTypes.put("doc", "application/msword"); contentTypes.put("xls", "application/vnd.ms-excel"); contentTypes.put("ppt", "application/vnd.ms-powerpoint"); contentTypes.put("pdf", "application/pdf"); contentTypes.put("exe", "application/octet-stream"); contentTypes.put("bin", "application/octet-stream"); contentTypes.put("zip", "application/zip"); contentTypes.put("htm", "text/html"); contentTypes.put("html", "text/html"); contentTypes.put("gif", "image/gif"); contentTypes.put("bmp", "image/bmp"); contentTypes.put("jpeg", "image/jpeg"); contentTypes.put("mpeg", "video/mpeg"); } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } @Override public String execute() throws Exception { inputStream = ServletActionContext.getServletContext().getResourceAsStream("/" + fileName); contentType = getContentType(fileName); return SUCCESS; } private String getContentType(String fileName){ for(String ext:contentTypes.keySet()){ if(fileName.endsWith(ext)){ return contentTypes.get(ext); } } return "application/octet-stream"; } } struts.xml配置文件: <package name="download" namespace="/" extends="struts-default"> <action name="download" class="cn.struts.FileDownload"> <result type="stream"> <param name="contentDisposition"> attachment;filename=${fileName} </param> <param name="inputName">inputStream</param> <param name="contentType"> ${contentType} </param> </result> </action> </package>