JSP文件下载

<%@ 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>

 

你可能感兴趣的:(jsp,文件下载)