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[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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值