1、jsp
<span style="font-size:12px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title></title>
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="author" content="">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="">
</head>
<script src="<%=request.getContextPath()%>/man/js/ajaxfileupload.js"></script>
<%@ include file="/WEB-INF/views/treeogrist.jsp"%>
<input type="hidden" id="treeCZTypeId">
<body >
<div class="page005">
<div class="row">
<div class="col-xs-9">
<div class="panel panel-ui">
<div class="panel-heading">
<form class="form-inline">
<div class="form-group">
<a href="<%=request.getContextPath()%>/download/excel.html?fileName=团队激励宝-产值事件下载模板.xls"
target=" _blank" class="download action">下载事件模板</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
</span>
2、controller
<span style="font-size:12px;">package system;
@Controller
@RequestMapping(value="/download")
public class DownloadController {
private Logger log = Logger.getLogger(DownloadController.class);
@RequestMapping(value="excel")
public void excel(ModelAndView mav,HttpServletRequest request,HttpServletResponse response) {
final String userAgent = request.getHeader("USER-AGENT");
String fileName =request.getParameter("fileName");
if(StringUtils.isBlank(fileName)){
return;
}
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String finalFileName = null;
File f = new File(this.getClass().getResource("").getPath());
String filepath = f.toString().split("WEB-INF")[0]+"excelmodel"+File.separator+fileName;
// System.out.println(filepath);
File file = new File(filepath);
byte[] b= new byte[1024];
int len = 0;
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
finalFileName = URLEncoder.encode(fileName,"UTF8");
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
finalFileName = new String(fileName.getBytes(), "ISO8859-1");
}else{
finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器
}
String Filename = finalFileName.substring(0, finalFileName.length()-4)+
(new SimpleDateFormat("yyyyMMdd").format(new Date()))+".xls";
response.addHeader("Content-Disposition","attachment; filename=" + Filename);
response.setContentLength( (int) file.length( ) );
while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
</span>