一.显示下载的文件资源
要将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" , "您要下载的资源已被删除!!" ); 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); } |
以上文件下载完成。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。