下載(W11DownLoadServlet.java和downlist.jsp和Downservlet.java)

:(W11DownLoadServlet.java和downlist.jsp和Downservlet.java)中介紹

1) ?DownLoadServlet.java中有關(guān)遍歷本地的資源,以及要轉(zhuǎn)發(fā)到downlist.jsp中展示,DownServlet.java中進(jìn)行下載代碼的實(shí)現(xiàn)

1.1)DownLoadServlet.java中有關(guān)遍歷本地的資源:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//response.getWriter().append("Served at: ").append(request.getContextPath());//保存文件的完整名稱(key)和簡(jiǎn)化名稱(value)MapfileNames = new HashMap<>();

//獲取目錄中所有文件

String path = "D:/upload";

File dirFile = new File(path);

//沒有判斷文件夾的情況。

if (dirFile!=null||dirFile.isDirectory()) {

String[] list = dirFile.list();

if (list!=null&&list.length>0) {

for (int i = 0; i < list.length; i++) {

String fullName = list[i];

String shortName = fullName.substring(fullName.indexOf("-")+1);

fileNames.put(fullName, shortName);

}

}

}

request.setAttribute("fileMap", fileNames);

//通過轉(zhuǎn)發(fā)調(diào)到界面

request.getRequestDispatcher("/downlist.jsp").forward(request, response);

}


1.2)轉(zhuǎn)發(fā)到downlist.jsp中展示

1.3)DownServlet.java中進(jìn)行下載代碼的實(shí)現(xiàn)

:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

String fileName = request.getParameter("name");

//處理get方式的中文亂碼

fileName = new String(fileName.getBytes("iso-8859-1"),"utf8");

String path = "D:/upload";

//獲取輸入流

InputStream in = new FileInputStream(new File(path,fileName));

//對(duì)URL的處理,不處理文件名中 中文會(huì)變成“ ”等形式

fileName=URLEncoder.encode(fileName,"utf8");

//設(shè)置下載的響應(yīng)頭,請(qǐng)求的內(nèi)容存為文件時(shí),需要一個(gè)默認(rèn)的文件名

response.setHeader("content-disposition", "attachment;fileName="+fileName);

//獲得響應(yīng)的輸出流

ServletOutputStream out = response.getOutputStream();

//將輸入流中的數(shù)據(jù)通過輸出流進(jìn)行保存

byte[] buff = new byte[1024];

int len=-1;

while ((len=in.read(buff))!=-1) {

out.write(buff,0,len);

}

out.close();

in.close();

}

注:********

//獲取輸入流

InputStream in = new FileInputStream(new File(path,fileName));


//對(duì)URL的處理,不處理文件名中 中文會(huì)變成“ ”等形式

fileName=URLEncoder.encode(fileName,"utf8");


//設(shè)置下載的響應(yīng)頭,請(qǐng)求的內(nèi)容存為文件時(shí),需要一個(gè)默認(rèn)的文件名

response.setHeader("content-disposition", "attachment;fileName="+fileName);



//獲得響應(yīng)的輸出流

ServletOutputStream out = response.getOutputStream();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,754評(píng)論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,868評(píng)論 18 139
  • 本文包括:1、文件上傳概述2、利用 Commons-fileupload 組件實(shí)現(xiàn)文件上傳3、核心API——Dis...
    廖少少閱讀 12,587評(píng)論 5 91
  • 1、不安全的隨機(jī)數(shù)生成,在CSRF TOKEN生成、password reset token生成等,會(huì)造成toke...
    nightmare丿閱讀 3,738評(píng)論 0 1
  • 突然間就天昏地暗 莫名的失落感((?(//?Д/?/)?))
    我的老天鵝閱讀 128評(píng)論 1 0