本文實(shí)例為大家分享了jsp實(shí)現(xiàn)文件下載功能的3種方法,供大家參考,具體內(nèi)容如下
第一種、采用轉(zhuǎn)發(fā)的方式:
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/1/10213.jpg";//即將下載的文件的相對路徑
String filedisplay = "10213.jpg";//下載文件時(shí)顯示的文件保存名稱
response.setContentType("application/x-download");//設(shè)置為下載application/x-download
//response.setContentType("application/x-msdownload");//設(shè)置為下載application/x-msdownload
//response.setContentType("application/octet-stream");//設(shè)置為下載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();
}
}
}
二、通過輸出流的方式:
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 {
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");
String filedisplay = "helloworld.jpg";
String filedownload = basePath + File.separator + "helloworld.jpg";
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();
}
}
第三種、通過超鏈接的方式(注意不推薦,因?yàn)闀┞断螺d文件的位置)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- vue實(shí)現(xiàn)在線預(yù)覽pdf文件和下載(pdf.js)
- 使用 JavaScript 創(chuàng)建并下載文件(模擬點(diǎn)擊)
- js使用文件流下載csv文件的實(shí)現(xiàn)方法
- 通過JavaScript下載文件到本地的方法(單文件)
- Node.js 使用request模塊下載文件的實(shí)例
- JS實(shí)現(xiàn)百度網(wǎng)盤任意文件強(qiáng)制下載功能
- Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)
- JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
- JavaScript實(shí)現(xiàn)文件下載并重命名代碼實(shí)例