一.顯示下載的文件資源
要將Web應用系統中的文件資源提供給用戶進行下載,首先我們要有一個頁面列出上傳文件目錄下的所有文件,當用戶點擊文件下載超鏈接時就進行下載操作,編寫一個ListFileServlet,用于列出Web應用系統中所有下載文件。
1.1 文件下載頁面
download.html代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<!DOCTYPE HTML> < html > < head > < title >下載文件顯示頁面</ title > </ head > < body > < div id = "fileName" ></ div > </ body > < script > $(function(){ download(); }); function download(){ $.ajax({ url: 'cloud/load/download', type: 'POST', dataType:'JSON', cache: false, processData: false, contentType: false, success : function(date){ var file=""; $.each(date,function(key,values){ var newKey = "/D:/Download/"+key; file += "< div >"+key+" "+"< a href = 'cloud/load/downloadFile?fileName="+key+"' >"+"下載"+"</ a >"+"</ div >"+"< br >"; $(values).each(function(){ file+="\t"+this; }); }); alert("success"); }, error : function(e){ alert("error"); } }); } </ script > </ html > |
1.2 controller
1
2
3
4
5
6
7
|
@RequestMapping (value = "/download" , method = RequestMethod.POST) @ResponseBody public Map<String,String> download(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, IOException{ Map<String,String> map = fileLoadService.doGet(request, response); return map; } |
1.3 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * 文件下載顯示 * @ClassName: FileLoadServiceImpl * @throws IOException * @throws ServletException */ @Override public Map<String,String> doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //獲取上傳文件的目錄 String uploadFilePath = "/D:/Download/" ; //存儲要下載的文件名 Map<String,String> fileNameMap = new HashMap<String,String>(); //遞歸遍歷filepath目錄下的所有文件和目錄,將文件的文件名存儲到map集合中 listfile( new File(uploadFilePath),fileNameMap); return fileNameMap; } public void listfile(File file,Map<String,String> map){ //如果file代表的不是一個文件,而是一個目錄 if (!file.isFile()){ //列出該目錄下的所有文件和目錄 File files[] = file.listFiles(); //遍歷files[]數組 for (File f : files){ //遞歸 listfile(f,map); } } else { String realName = file.getName().substring(file.getName().indexOf( "_" )+ 1 ); //file.getName()得到的是文件的原始名稱,這個名稱是唯一的,因此可以作為key,realName是處理過后的名稱,有可能會重復 map.put(file.getName(), realName); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } |
二.下載顯示的文件資源
2.1 controller
1
2
3
4
5
6
|
@RequestMapping (value = "/downloadFile" , method = RequestMethod.GET) @ResponseBody public void downloadFile(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, IOException{ String filename =request.getParameter( "fileName" ); fileLoadService.doGetFile(request, response ,filename); } |
2.2 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/** * 下載文件到本地 start */ @Override public void doGetFile(HttpServletRequest request, HttpServletResponse response,String filename) throws ServletException,IOException { //得到要下載的文件名 String fileName = filename; fileName = new String(fileName.getBytes( "iso8859-1" ), "UTF-8" ); String fileSaveRootPath= "/D:/Download" ; File file = new File(fileSaveRootPath + "/" + fileName); //如果文件不存在 if (!file.exists()){ request.setAttribute( "message" , "您要下載的資源已被刪除?。?quot; ); return ; } //處理文件名 String realname = fileName.substring(fileName.indexOf( "_" )+ 1 ); //設置響應頭,控制瀏覽器下載該文件 response.setHeader( "content-disposition" , "attachment;filename=" + URLEncoder.encode(realname, "UTF-8" )); InputStream fis = new BufferedInputStream( new FileInputStream(fileSaveRootPath + "\\" + fileName)); byte [] buffer = new byte [fis.available()]; fis.read(buffer); //讀取文件流 fis.close(); response.reset(); //重置結果集 response.setHeader( "content-disposition" , "attachment;filename=" + URLEncoder.encode(realname, "UTF-8" )); response.addHeader( "Content-Length" , "" + file.length()); //返回頭 文件大小 response.setContentType( "application/octet-stream" ); //設置數據種類 OutputStream os = new BufferedOutputStream(response.getOutputStream()); os.write(buffer); // 輸出文件 os.flush(); os.close(); } public void doPostFile(HttpServletRequest request, HttpServletResponse response,String filename) throws ServletException, IOException { doGetFile(request, response,filename); } |
以上文件下載完成。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。