Web Dev Notes - 11 - 实现web服务器文件列表并包含文件下载

本文详细介绍了如何使用JSP代码实现文件列表显示和文件下载功能,包括服务器路径获取、文件过滤与链接生成。

原文链接:http://hehaiztc.iteye.com/blog/682340


Jsp代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@page import="java.io.*"%>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.     <title>列表显示</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">      
  12.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  13.     <meta http-equiv="description" content="This is my page">  
  14.     <!--  
  15.     <link rel="stylesheet" type="text/css" href="styles.css">  
  16.     -->  
  17.   
  18.   </head>  
  19.   
  20. <body>  
  21. <%  
  22.     String tempPath1 = "spjk"+"\\"+"video"+"\\";//指定目录(本地)  
  23.     String tempPath2 = "spjk"+"/"+"video"+"/";//指定目录(url)  
  24.           
  25.     String realpath = request.getSession().getServletContext().getRealPath("\\");//服务器绝对路径  
  26.     realpath = realpath + tempPath1;  
  27.       
  28.     String path = request.getContextPath();  
  29.     String basepath = request.getScheme()+"://"+request.getServerName()  
  30.                     +":"+request.getServerPort()+path+"/";//服务器Url路径  
  31.     basepath = basepath + tempPath2;  
  32.       
  33.       
  34.     System.out.println(realpath);  
  35.     System.out.println(basepath);     
  36.     File f = new File(realpath);  
  37.     if (!f.exists())  
  38.     {  
  39.         out.println(basepath+"查无文件");  
  40.         return;  
  41.     }  
  42.     File fa[] = f.listFiles();  
  43.     for(int i=0;i<fa.length;i++)  
  44.     {  
  45.         File fs = fa[i];  
  46.         if (!fs.isDirectory())  
  47.         {  
  48.             out.println("<a target=_blank "+"href=download.jsp?filepath="+realpath+fs.getName()+"&filename="+fs.getName()+">"+fs.getName()+"</a>");  
  49.             System.out.println("<a target=_blank "+"href=download.jsp?filepath="+realpath+fs.getName()+"&filename="+fs.getName()+">"+fs.getName()+"</a>");  
  50.               
  51.             out.println("<br />");  
  52.         }  
  53.         else{  
  54.         }  
  55.     }  
  56. %>  
  57. </body>  
  58. </html>  


下载jsp 
download.jsp 
Jsp代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@page import="java.io.*"%>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.     <title>文件下载</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">      
  12.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  13.     <meta http-equiv="description" content="This is my page">  
  14.     <!--  
  15.     <link rel="stylesheet" type="text/css" href="styles.css">  
  16.     -->  
  17.   
  18.   </head>  
  19.   
  20. <body>  
  21. <%  
  22.     try  
  23.     {  
  24.         //接受下载路径和文件名称  
  25.         String filepath = request.getParameter("filepath");  
  26.         String filename = request.getParameter("filename");  
  27.         if (filepath == null||filename ==null)    
  28.         {  
  29.             out.println("请选择要下载的文件!");  
  30.             return;    
  31.         }         
  32.         //获得读取本地文件的输入流            
  33.         FileInputStream fin = new FileInputStream(filepath);  
  34.         //设置响应的MIMI类型         
  35.         response.setContentType("application/force-download");            
  36.         response.addHeader("Content-Disposition""attachment;filename="+filename);   
  37.         //流的方式输出文件  
  38.         byte[] buf = new byte[1024];  
  39.         int readSize = fin.read(buf);  
  40.         OutputStream os = response.getOutputStream();  
  41.           
  42.         while(readSize != -1) {  
  43.             os.write(buf, 0, readSize);  
  44.             readSize = fin.read(buf);  
  45.         }  
  46.         os.flush();  
  47.         os.close();  
  48.         os = null ;  
  49.         response.flushBuffer();  
  50.         out.clear();  
  51.         out  =  pageContext.pushBody();          
  52.     }  
  53.     catch(Exception e){  
  54.         e.printStackTrace();   
  55.     }  
  56.   
  57. %>  
  58. </body>  
  59. </html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值