原文链接:http://hehaiztc.iteye.com/blog/682340
- <%@ 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[i];
- 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[1024];
- 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>
本文详细介绍了如何使用JSP代码实现文件列表显示和文件下载功能,包括服务器路径获取、文件过滤与链接生成。


被折叠的 条评论
为什么被折叠?



