Skip to content

Instantly share code, notes, and snippets.

@tdoly
Created October 8, 2013 07:47
Show Gist options
  • Save tdoly/6881100 to your computer and use it in GitHub Desktop.
Save tdoly/6881100 to your computer and use it in GitHub Desktop.
很早之前写了个用jsp显示主机上文件的程序。输入路径,显示文件和文件夹的名称,修改时间,大小。如果是文件,可以下载
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" import="java.lang.*" import=" javax.servlet.ServletOutputStream" errorPage="" %>
<%
request.setCharacterEncoding("gb2312");
String filename=request.getParameter("filename");
String filepath=request.getParameter("filepath");
filename=java.net.URLDecoder.decode(filename);
filepath=java.net.URLDecoder.decode(filepath);
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment;filename=\"" + new String(filename.getBytes("gb2312"),"iso8859-1") + "\"");
OutputStream outputStream=null;
FileInputStream fileInputStream=null;
try{
outputStream=response.getOutputStream();
fileInputStream=new FileInputStream(filepath + filename);
byte[] b=new byte[1024];
int i=0;
while((i=fileInputStream.read(b)) > 0) {
outputStream.write(b, 0, i);
}
outputStream.flush();
out.clear();
out=pageContext.pushBody();
} catch(Exception e) {
e.printStackTrace();
} finally {
if(fileInputStream != null) {
fileInputStream.close();
fileInputStream=null;
}
}
%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" import="java.lang.*" import="java.util.Date" import="java.text.*" errorPage="" %>
<html>
<head>
<title>Directory</title>
<script type="text/javascript">
function doSave(){
var path=document.getElementById("inputPath");
if("" == path.value){
alert("请输入正确的路径");
return;
}
}
</script>
</head>
<style type="text/css">
/* CSS Document */
body {
font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
background: #E6EAE9;
}
a {
color: #c75f3e;
}
#mytable {
width: 700px;
padding: 0;
margin: 0;
}
caption {
padding: 0 0 5px 0;
width: 700px;
font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
text-align: right;
}
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
}
/*power by www.winshell.cn*/
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}
td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size:11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
/*power by www.winshell.cn*/
td.alt {
background: #F5FAFA;
color: #797268;
}
th.spec {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #fff no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
}
th.specalt {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #f5fafa no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #797268;
}
/*---------for IE 5.x bug*/
html>body td{ font-size:11px;}
body,td,th {
font-family: 宋体, Arial;
font-size: 12px;
}
</style>
<%! File file=null;
//获取文件夹大小
public static long getFileSizes(File f) {
long size=0;
File[] fileList=f.listFiles();
for(int i=0; i<fileList.length; i++) {
if(fileList[i].isDirectory()) {
size=size + getFileSizes(fileList[i]);
} else {
size=size + fileList[i].length();
}
}
return size;
}
//文件大小转换
public static String formatFileSize(long fileSize) {
DecimalFormat df=new DecimalFormat("#.00");
String fileSizeString="";
if(fileSize < 1024) {
fileSizeString=df.format((double)fileSize) + "B";
} else if(fileSize < 1024 *1024) {
fileSizeString=df.format((double)fileSize / 1024) + "K";
} else if(fileSize < 1024 * 1024 * 1024) {
fileSizeString=df.format((double)fileSize / (1024 * 1024)) + "M";
} else {
fileSizeString=df.format((double)fileSize / (1024 * 1024 * 1024)) + "G";
}
return fileSizeString;
}
%>
<%
request.setCharacterEncoding("gb2312");
String url=request.getParameter("URL"); // URL存放本地文件的相对路径!
String inputPath=request.getParameter("inputPath"); //获取输入框中的路径
String title=""; // 存储文件夹的名字
String path=""; // “path”参数是用来构造文件的路径参数。
session.setMaxInactiveInterval(-1); // 使用了Session来保存一个常量initpath,把session的生命期设为无限,防止出现超时现象。
if(inputPath == null || inputPath.length() == 0 || !inputPath.contains(":")) { //判断输入的路径是否为空
title="请输入路径";
%>
<body>
<div align="center"><h1><font color="blue"><%=title %></font></h1>
<form name="getPath" id="getPath" method="post" action="">
<input type="text" name="inputPath" id="inputPath" size="50" value="<%=path%>"></input>
<input type="submit" name="submit" value="进入" onclick="doSave()"></input>
</form>
<%
return;
} else {
String folderSize=""; // 格式化之后文件夹的大小
String fileSize=""; // 格式化之后文件的大小
int len=0; // 记录路径的长度
long l=0; // 记录文件及文件夹的大小
if(url == null) {
String inipath=new String(inputPath.getBytes("iso-8859-1"),"gb2312"); // 作为常量驻于Session中。与URL参数加进来就是一个文件的完整路径
len=inipath.length(); // 存储在session里的路径长度
session.setAttribute("Path", inipath);
session.setAttribute("Len", len);
path=inipath;
title=path.substring(path.lastIndexOf("\\")+1);
} else {
url=java.net.URLDecoder.decode(url); // URL参数有可能是中文参数,
path=session.getAttribute("Path")+url+"\\";
String Len=session.getAttribute("Len")+"";
len=Integer.parseInt(Len);
if(url.lastIndexOf("\\")!=-1) {
title=url.substring(url.lastIndexOf("\\")+1);
} else {
title=url;
}
}
file=new File(path); //建立起一个文件实例
File[] files=file.listFiles(); //该文件是个目录,列出其下的文件
%>
<body>
<div align="center"><h1><font color="blue"><%=title %></font></h1>
<input type="text" name="inputPath" id="inputPath" size="50" value="<%=path%>"></input>
<input type="submit" name="submit" value="返回" onclick="history.back()"></input>
<table width="880" border=0 cellpadding="0" cellspacing="1">
<tr>
<th scope="col">文件</th>
<th scope="col">修改时间</th>
<th scope="col">类型</th>
<th scope="col">大小</th>
<th scope="col">下载</th>
</tr>
<%
for(int i=0;i<files.length;i++) {
Date date=new Date(files[i].lastModified());
String lstmdfy=String.valueOf(date.getYear()+1900)+"年"+String.valueOf(date.getMonth()+1)+"月"+String.valueOf(date.getDate())+"日";
String dirPath="";
if(files[i].isDirectory()&&!files[i].isHidden()) {
dirPath=files[i].toString();
File ff=new File(dirPath);
if(ff.isDirectory()) {
l=getFileSizes(ff);
}
folderSize=formatFileSize(l);
String str=files[i].getAbsolutePath().substring(len);
out.println("<tr><td class='row'><span>"+"<a href='index.jsp?URL="+java.net.URLEncoder.encode(str)+"&inputPath="+path+"'>"+files[i].getName()+"</a></span></td><td class='row'><span>"+lstmdfy+"</span></td><td class='row'><span>目录</span></td><td class='row'><span>"+folderSize+"</span></td></tr>");
}
if(!files[i].isDirectory()&&!files[i].isHidden()) {
long lsize=files[i].length();
fileSize=formatFileSize(lsize);
out.println("<tr><td class='row'><span>"+files[i].getName()+"</span></td><td class='row'><span>"+lstmdfy+"</span></td><td class='row'><span>文件</span></td><td class='row'><span>"+fileSize+"</span></td><td class='row'><span><a href='downloadFile.jsp?filename="+java.net.URLEncoder.encode(files[i].getName())+"&filepath="+java.net.URLEncoder.encode(path)+"'>下载</a></span></td></tr>");
}
}
}
%>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment