一个jsp的下载实例

作者在 2007-04-28 07:40:00 发布以下内容

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>

 

jsp | 阅读 1848 次
文章评论,共0条
游客请输入验证码
浏览57145次