您的位置:首页 > Web前端 > JavaScript

JSP文件下载

2012-03-05 12:30 323 查看
 
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
response.setCharacterEncoding("gb18030");
System.out.println("path1:" +request.getSession().getServletContext().getRealPath("/"));
String name = request.getSession().getServletContext().getRealPath("/").replace("\\","/") + "downloadfiles/outlook邮件归类.docx";
File downFile = new File(name);
response.setContentType("binary/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(downFile.getName().getBytes("gb2312"),
"iso8859-1"));
ServletOutputStream servletOutputStream = response
.getOutputStream();
InputStream is = new FileInputStream(downFile);
byte[] bytes = new byte[1024];
while (true) {
int chunk = is.read(bytes);
if (chunk == -1) {
break;
}
servletOutputStream.write(bytes, 0, chunk);
}
is.close();
%>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: