下载页面
<%@ 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=Java.pdf">下载</a>
</body>
</html>
struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 开启开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<package name="jiangwenwen" namespace="/" extends="struts-default">
<action name="download" class="cn.jiangwenwen.action.DownLoadAction" method="download">
<result name="success" type="stream">
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="contentLength">
${fileSize}
</param>
<param name="contentType">
${contentType}
</param>
</result>
</action>
</package>
</struts>
Action类
public class DownLoadAction extends ActionSupport{
private String contentType;
private String FileName;
private InputStream inputStream;
private long fileSize;
public String download() throws FileNotFoundException {
String rootpath = ServletActionContext.getServletContext().getRealPath("/");
System.out.println(rootpath);
System.out.println(FileName);
File file = new File(rootpath+"/download/"+FileName);
fileSize = file.length();
inputStream = new FileInputStream(file);
return SUCCESS;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return FileName;
}
public void setFileName(String fileName) {
FileName = fileName;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
}