相信下面幾個頁面很少有人愿意放到自己的網站上,寫出來僅供消遣了。
1.主頁面,List所有文件及文件夾
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.io.File"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Iterator"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String fileName = request.getParameter("fileName");
if (fileName == null) {
fileName = "/";
}
File file = new File(fileName);
String par = file.getParent();
out.println("<a href='?fileName=" + par + "'>Up ...</a><br><br>");
out.println("Current Path: " + fileName + "<br><br>");
Map<String, String> folders = getSubFolders(file);
Iterator<String> it = folders.keySet().iterator();
String key;
while (it.hasNext()) {
key = it.next();
out.println(" <a href='?fileName="
+ folders.get(key) + "'>" + key + "</a><br>");
}
Map<String, String> files = getSubFileList(file);
it = files.keySet().iterator();
while (it.hasNext()) {
key = it.next();
if (editable(key)) {
out
.println(" <a href='edit.jsp?fileName="
+ files.get(key)
+ "Action=Edit'><img src='images/edit.bmp' style='width: 15px; height: 15px;border: 0;'/></a><a href='printFile.jsp?fileName="
+ files.get(key) + "'>" + key + "</a><br>");
} else {
out.println(" " + key + "<br>");
}
}
%>
<%!Map<String, String> getSubFileList(File file) {
Map<String, String> result = new HashMap<String, String>();
if (file.listFiles() != null) {
for (File f : file.listFiles()) {
if (f.isFile()) {
result.put(f.getName(), f.getAbsolutePath());
}
}
}
return result;
}
Map<String, String> getSubFolders(File file) {
Map<String, String> result = new HashMap<String, String>();
if (file.listFiles() != null) {
for (File f : file.listFiles()) {
if (f.isDirectory()) {
result.put(f.getName(), f.getAbsolutePath());
}
}
}
return result;
}
boolean editable(String name) {
namename = name.toUpperCase();
if (name.endsWith(".JAVA") || name.endsWith(".XML")
|| name.endsWith(".LOG") || name.endsWith(".JSP")
|| name.endsWith(".HTM") || name.endsWith(".TXT")
|| name.endsWith(".HTML") || name.endsWith(".SQL")
|| name.endsWith(".POLICY") || name.endsWith(".PROPERTIES")
|| name.indexOf(".") <= 0 || name.endsWith(".BAT")
|| name.endsWith(".SH") || name.endsWith(".CSS")
|| name.endsWith(".JS")) {
return true;
} else {
return false;
}
}%>
</body>
</html>
2.打印文本文件頁面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.io.FileInputStream"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
String fileName = request.getParameter("fileName");
%>
<title><%=fileName%></title>
</head>
<body>
<a href="javascript:history.go(-1);">Back</a>
<br>
<br>
<pre>
<%
try{
FileInputStream input = new FileInputStream(fileName);
byte[] buff = new byte[1024];
int n = 0;
while ((n = input.read(buff)) != -1) {
out.println(toHtmlText(new String(buff, 0, n)));
}
input.close();
}catch(Exception ex){
}
%>
<%!String toHtmlText(String s) {
if (s == null)
return s;
s = strReplace(s, "&", "&");
s = strReplace(s, "<", "<");
s = strReplace(s, ">", ">");
s = strReplace(s, "/"", """);
s = strReplace(s, " ", " ");
return s;
}
String strReplace(String sBody, String sFrom, String sTo) {
int i, j, k, l;
i = 0;
l = 0;
j = sFrom.length();
k = sTo.length();
if (k < j)
k = sBody.length();
else
k = sBody.length() * 2;
StringBuffer ret = new StringBuffer(k);
while (sBody.indexOf(sFrom, i) != -1) {
ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
i = sBody.indexOf(sFrom, i);
i += j;
}
ret.append(sBody.substring(i));
return ret.toString();
}%>
</pre>
</body>
</html>
3.文本文件修改頁面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@page import="java.io.File"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.BufferedWriter"%>
<%@page import="java.io.OutputStreamWriter"%>
<%@page import="java.io.FileInputStream"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Editing</title>
</head>
<body>
<%
String fileName = request.getParameter("fileName");
String content = request.getParameter("content");
String action = request.getParameter("Action");
StringBuilder sb = new StringBuilder("");
String title = "";
try {
if("Edit".equals(action)){
FileInputStream input = new FileInputStream(fileName);
byte[] buff = new byte[1024];
int n = 0;
while ((n = input.read(buff)) != -1) {
sb.append(toHtmlText(new String(buff, 0, n)));
}
title = fileName;
input.close();
}else if(fileName != null) {
System.out.println("Saving!");
File file = new File(fileName);
OutputStream stream = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(stream, "utf-8"));
writer.write(content);
writer.close();
stream.close();
}else{
sb.append("");
}
} catch (Exception ex) {
ex.printStackTrace();
}
%>
<form action="edit.jsp?Action=Save" method="POST">File Name<input type="text"
id="fileName" name="fileName" value="<%=title %>"><br>
Content<textarea rows="35" cols="150" id="content" name="content"><%=sb.toString() %></textarea><br>
<input type="submit" value="Save"></form>
</body>
<%!String toHtmlText(String s) {
if (s == null)
return s;
s = strReplace(s, "&", "&");
s = strReplace(s, "<", "<");
s = strReplace(s, ">", ">");
s = strReplace(s, "/"", """);
s = strReplace(s, " ", " ");
return s;
}
String strReplace(String sBody, String sFrom, String sTo) {
int i, j, k, l;
i = 0;
l = 0;
j = sFrom.length();
k = sTo.length();
if (k < j)
k = sBody.length();
else
k = sBody.length() * 2;
StringBuffer ret = new StringBuffer(k);
while (sBody.indexOf(sFrom, i) != -1) {
ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
i = sBody.indexOf(sFrom, i);
i += j;
}
ret.append(sBody.substring(i));
return ret.toString();
}%>
</html>