1.输入进入的目录
<body>
<h1>输入读取的目录</h1>
<input
id="filePath"
name="filePath"/>
<input
type="button"
value="提交"
onclick="location.href='<%=request.getContextPath()%>/logList.jsp?filePath='+encodeURIComponent(encodeURIComponent(document.getElementById('filePath').value))"/>
</body>
2.得到目录列表
<body>
<%
String filePath =request.getParameter("filePath");
filePath = URLDecoder.decode(URLDecoder.decode(filePath,"utf-8"),
"utf-8");
File dir = new
File(filePath);
if (dir.exists()) {
//获取该目录下的所有文件和目录组成的File数组
File[] files =dir.listFiles();
//递归遍历每一个子文
for
(File file : files) {
%>
<ahref="<%=request.getContextPath()%>/logDetail.jsp?filePath=<%=URLEncoder.encode(URLEncoder.encode(file.getPath(),"utf-8"),"utf-8")%>"><%=file.getName()%>
</a>;<br>
<%}
%>
<inputtype="button"
value="返回上一层"
onclick="history.back()">
<%
}
%>
</body>
3.详情页面,如果是目录进入目录,如果是文件则打开
<body>
<%
String filePath =request.getParameter("filePath");
filePath = URLDecoder.decode(URLDecoder.decode(filePath,"utf-8"),
"utf-8");
String fileFullName = filePath;
%>
<h5><%=fileFullName%>
</h5>
<p>
<%
File file =
new File(fileFullName);
if (file.exists()) {
//如果是目录,则:
if
(file.isDirectory()) {
%>
<a
href="<%=request.getContextPath()%>/logList.jsp?filePath=<%=URLEncoder.encode(URLEncoder.encode(file.getPath(),"utf-8"),"utf-8")%>"><%=file.getName()%>
<input
type="button"
value="进入"
onclick="history.back()">
<%
} else
{
InputStreamReader read = new
InputStreamReader(
new
FileInputStream(file),
"gbk");//考虑到编码格式
BufferedReaderbufferedReader =
new BufferedReader(read);
String lineTxt = null;
while
((lineTxt =bufferedReader.readLine()) !=
null) {
%>
<%=lineTxt%><br>
<%
}
read.close();
%>
<input
type="button"
value="返回上一层"
onclick="history.back()">
<%
}
} else {
%>
无内容<br>
<%
}
%>
</p>
</body>