`

jsp中文档文件下载

    博客分类:
  • JSP
阅读更多
<%@page import="java.net.URLEncoder"%>
<%@page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="application/msword;charset=UTF-8"%>
<%
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>文档下载</title>
</head>

<body>
<%
String para = request.getParameter("a");
response.setContentType("application/x-download");
//文件路径
String filePath = "/" + para;
String displayName = para;
displayName = URLEncoder.encode(displayName, "UTF-8");
//设置下载文件保存名称
response.addHeader("Content-Disposition", "attachment;filename="
+ displayName);
try {
RequestDispatcher dis = application
.getRequestDispatcher(filePath);
if (dis != null) {
dis.forward(request, response);
}
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
//getOutputStream() has already been called for this response异常出现解决方法
out.clear();
out = pageContext.pushBody();
}
%>
<br>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics