Servlet 代码:
String filenamedownloa = request.getParameter("url");
String filenamedownload = new String(filenamedownloa.getBytes("iso8859-1"),"gbk");
response.setContentType("application/x-download");// 设置为下载application/x-download
String filenamedisplay = filenamedownload.substring(0, filenamedownload.length());
filenamedisplay = URLEncoder.encode(filenamedisplay, "utf-8");
response.addHeader("Content-Disposition","attachment;filename="+filenamedisplay);
try {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(filenamedownload);
if (dispatcher != null) {
dispatcher.forward(request,response);
}
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
htm代码:
<html>
<head>
<title>Testfiledownload.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
</head>
<body>
<a href="servlet/DownloadFileServlet?url=/temp2/1.doc">filedownload</a>
</body>
</html>