hehaiztc 发表于 2013-1-15 02:28:56

jsp实现web服务器文件列表并包含文件下载(更新中)

参考了一些网上别人写的内容,写了如下jsp,记下备用。

工程有2个jsp页面,list.jsp(实现列表功能,这里默认打开WebRoot/spjk/video文件夹)
download.jsp(实现下载)

显示web服务器目录下所有文件列表jsp:
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@page import="java.io.*"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>    <title>列表显示</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%String tempPath1 = "spjk"+"\\"+"video"+"\\";//指定目录(本地)String tempPath2 = "spjk"+"/"+"video"+"/";//指定目录(url)String realpath = request.getSession().getServletContext().getRealPath("\\");//服务器绝对路径realpath = realpath + tempPath1;String path = request.getContextPath();String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";//服务器Url路径basepath = basepath + tempPath2;System.out.println(realpath);System.out.println(basepath);File f = new File(realpath);if (!f.exists()){out.println(basepath+"查无文件");return;}File fa[] = f.listFiles();for(int i=0;i<fa.length;i++){File fs = fa;if (!fs.isDirectory()){out.println("<a target=_blank "+"href=download.jsp?filepath="+realpath+fs.getName()+"&filename="+fs.getName()+">"+fs.getName()+"</a>");System.out.println("<a target=_blank "+"href=download.jsp?filepath="+realpath+fs.getName()+"&filename="+fs.getName()+">"+fs.getName()+"</a>");out.println("<br />");}else{}}%></body></html>

下载jsp
download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@page import="java.io.*"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>    <title>文件下载</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%try{//接受下载路径和文件名称String filepath = request.getParameter("filepath");String filename = request.getParameter("filename");if (filepath == null||filename ==null){out.println("请选择要下载的文件!");return;}//获得读取本地文件的输入流      FileInputStream fin = new FileInputStream(filepath);//设置响应的MIMI类型      response.setContentType("application/force-download");      response.addHeader("Content-Disposition", "attachment;filename="+filename);    //流的方式输出文件    byte[] buf = new byte;    int readSize = fin.read(buf);    OutputStream os = response.getOutputStream();      while(readSize != -1) {      os.write(buf, 0, readSize);      readSize = fin.read(buf);    }    os.flush();    os.close();    os = null ;    response.flushBuffer();    out.clear();    out=pageContext.pushBody();      }catch(Exception e){e.printStackTrace(); }%></body></html>
页: [1]
查看完整版本: jsp实现web服务器文件列表并包含文件下载(更新中)