文章詳情頁(yè)
jsp文件下載功能實(shí)現(xiàn)代碼
瀏覽:781日期:2022-06-07 09:14:49
本文實(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";//即將下載的文件的相對(duì)路徑
String filedisplay = "10213.jpg";//下載文件時(shí)顯示的文件保存名稱(chēng)
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();
}
}
}
二、通過(guò)輸出流的方式:
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();
}
}
第三種、通過(guò)超鏈接的方式(注意不推薦,因?yàn)闀?huì)暴露下載文件的位置)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐2. jsp實(shí)現(xiàn)登錄界面3. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲6. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶(hù)未登錄訪問(wèn)8. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法9. JSP之表單提交get和post的區(qū)別詳解及實(shí)例10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
排行榜

網(wǎng)公網(wǎng)安備