日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jsp中文件下载的实现

發布時間:2025/1/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jsp中文件下载的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

方式一:采用RequestDispatcher進行

[java]?view plaincopy
  • package?cn.jbit.download.servlet;??
  • ??
  • import?java.io.IOException;??
  • ??
  • import?javax.servlet.RequestDispatcher;??
  • import?javax.servlet.ServletException;??
  • import?javax.servlet.http.HttpServlet;??
  • import?javax.servlet.http.HttpServletRequest;??
  • import?javax.servlet.http.HttpServletResponse;??
  • ??
  • public?class?DownloadServlet?extends?HttpServlet?{??
  • ??
  • ????private?static?final?long?serialVersionUID?=?6765085208899952414L;??
  • ??
  • ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????doPost(request,?response);??
  • ????}??
  • ??
  • ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????String?filedownload?=?"/upload/helloworld.jar";//即將下載的文件的相對路徑??
  • ????????String?filedisplay?=?"helloworld.jar";//下載文件時顯示的文件保存名稱??
  • ????????response.setContentType("application/x-download");//設置為下載application/x-download??
  • ????????//response.setContentType("application/x-msdownload");//設置為下載application/x-msdownload??
  • ????????//response.setContentType("application/octet-stream");//設置為下載application/octet-stream??
  • ????????response.addHeader("Content-Disposition",?"attachment;filename="??
  • ????????????????+?filedisplay);??
  • ??????????
  • ????????try?{??
  • ????????????RequestDispatcher?rd?=?request.getRequestDispatcher(filedownload);??
  • ????????????if(rd?!=?null)??
  • ????????????{??
  • ????????????????rd.forward(request,response);??
  • ????????????}??
  • ????????????response.flushBuffer();??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????}??
  • }??
  • 方式二:采用文件流輸出的方式下載

    [java]?view plaincopy
  • package?cn.jbit.download.servlet;??
  • ??
  • import?java.io.BufferedInputStream;??
  • import?java.io.BufferedOutputStream;??
  • import?java.io.File;??
  • import?java.io.FileInputStream;??
  • import?java.io.IOException;??
  • import?java.io.InputStream;??
  • import?java.io.OutputStream;??
  • ??
  • import?javax.servlet.ServletException;??
  • import?javax.servlet.http.HttpServlet;??
  • import?javax.servlet.http.HttpServletRequest;??
  • import?javax.servlet.http.HttpServletResponse;??
  • ??
  • public?class?DownloadOfIOServlet?extends?HttpServlet?{??
  • ??
  • ????private?static?final?long?serialVersionUID?=?6765085208899952414L;??
  • ??
  • ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????doPost(request,?response);??
  • ????}??
  • ??????
  • ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????String?basePath?=?request.getSession().getServletContext().getRealPath("/upload");??
  • ????????//System.out.println(basePath);??
  • ????????String?filedisplay?=?"helloworld.jar";??
  • ????????String?filedownload?=?basePath?+?File.separator?+?"helloworld.jar";??
  • ????????System.out.println("----------------------"+filedownload);??
  • ????????response.setContentType("applicaiton/x-download");??
  • ????????response.addHeader("Content-Disposition",?"attachment;filename="+filedisplay);??
  • ??????????
  • ????????InputStream?is?=?null;??
  • ????????OutputStream?os?=?null;??
  • ????????BufferedInputStream?bis?=?null;??
  • ????????BufferedOutputStream?bos?=?null;??
  • ??????????
  • ????????is?=?new?FileInputStream(new?File(filedownload));??
  • ????????bis?=?new?BufferedInputStream(is);??
  • ????????os?=?response.getOutputStream();??
  • ????????bos?=?new?BufferedOutputStream(os);??
  • ??????????
  • ????????byte[]?b?=?new?byte[1024];??
  • ????????int?len?=?0;??
  • ????????while((len?=?bis.read(b))?!=?-1){??
  • ????????????bos.write(b,0,len);??
  • ????????}??
  • ??????????
  • ????????bis.close();??
  • ????????is.close();??
  • ????????bos.close();??
  • ????????os.close();??
  • ????}??
  • }??
  • 方式三:網頁上做超級鏈接(不推薦使用,樣服務器上的目錄資源會直接暴露給最終用戶)

    [html]?view plaincopy
  • <a?href="/DownloadFile/upload/helloworld.jar">helloworld.jar</a>??
  • jsp頁面代碼:

    [html]?view plaincopy
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="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>My?JSP?'download.jsp'?starting?page</title>??
  • ????<meta?http-equiv="pragma"?content="no-cache">??
  • ????<meta?http-equiv="cache-control"?content="no-cache">??
  • ????<meta?http-equiv="expires"?content="0">??????
  • ??</head>??
  • ????
  • ??<body>??
  • ????下載1<a?href="/DownloadFile/upload/helloworld.jar">helloworld.jar</a><br/>??
  • ????下載2<a?href="/DownloadFile/servlet/downloadServlet">helloworld.jar</a><br/>??
  • ????下載3<a?href="/DownloadFile/servlet/downloadOfIOServlet">helloworld.jar</a><br/>??
  • ????下載4<a?href="/DownloadFile/download/filedownload.action">helloworld.jar</a><br/>??
  • ??</body>??
  • </html>??
  • 總結

    以上是生活随笔為你收集整理的jsp中文件下载的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。