ftpcontrol.java
来自「非常棒的java数据库」· Java 代码 · 共 413 行 · 第 1/2 页
JAVA
413 行
} else if ("NOOP".equals(command)) {
reply(200, "Ok");
}
break;
case 'P':
if ("PWD".equals(command)) {
reply(257, StringUtils.quoteIdentifier(currentDir) + " directory");
} else if ("PASV".equals(command)) {
ServerSocket dataSocket = server.createDataSocket();
data = new FtpData(server, control.getInetAddress(), dataSocket);
data.start();
int port = dataSocket.getLocalPort();
reply(227, "Passive Mode (" + serverIpAddress + "," + (port >> 8) + "," + (port & 255) + ")");
} else if ("PORT".equals(command)) {
String[] list = StringUtils.arraySplit(param, ',', true);
String host = list[0] + "." + list[1] + "." + list[2] + "." + list[3];
int port = (Integer.parseInt(list[4]) << 8) | Integer.parseInt(list[5]);
InetAddress address = InetAddress.getByName(host);
if (address.equals(control.getInetAddress())) {
data = new FtpData(server, address, port);
reply(200, "Ok");
} else {
server.log("Port REJECTED:" + address + " expected:" + control.getInetAddress());
reply(550, "Failed");
}
}
break;
case 'R':
if ("RNFR".equals(command)) {
String fileName = getFileName(param);
if (fs.exists(fileName)) {
renameFrom = fileName;
reply(350, "Ok");
} else {
reply(450, "Not found");
}
} else if ("RNTO".equals(command)) {
if (renameFrom == null) {
reply(503, "RNFR required");
} else {
String fileOld = renameFrom;
String fileNew = getFileName(param);
boolean ok = false;
if (!readonly) {
try {
fs.rename(fileOld, fileNew);
reply(250, "Ok");
ok = true;
} catch (SQLException e) {
server.logError(e);
}
}
if (!ok) {
reply(550, "Failed");
}
}
} else if ("RETR".equals(command)) {
String fileName = getFileName(param);
if (fs.exists(fileName) && !fs.isDirectory(fileName)) {
reply(150, "Starting transfer");
try {
data.send(fs, fileName, restart);
reply(226, "Ok");
} catch (IOException e) {
server.logError(e);
reply(426, "Failed");
}
restart = 0;
} else {
processList(param, true); // Firefox compatibility (still
// not good)
// reply(426, "Not a file");
}
} else if ("RMD".equals(command)) {
processRemoveDir(param);
} else if ("REST".equals(command)) {
try {
restart = Integer.parseInt(param);
reply(350, "Ok");
} catch (NumberFormatException e) {
reply(500, "Invalid");
}
}
break;
case 'S':
if ("SYST".equals(command)) {
reply(215, "UNIX Type: L8");
} else if ("SITE".equals(command)) {
reply(500, "Not understood");
} else if ("SIZE".equals(command)) {
param = getFileName(param);
if (fs.exists(param) && !fs.isDirectory(param)) {
reply(250, String.valueOf(fs.length(param)));
} else {
reply(500, "Failed");
}
} else if ("STOR".equals(command)) {
String fileName = getFileName(param);
if (!readonly && !fs.exists(fileName) || !fs.isDirectory(fileName)) {
reply(150, "Starting transfer");
try {
data.receive(fs, fileName);
if (server.getAllowTask() && param.endsWith(FtpServer.TASK_SUFFIX)) {
server.startTask(fileName);
}
reply(226, "Ok");
} catch (Exception e) {
server.logError(e);
reply(426, "Failed");
}
} else {
reply(550, "Failed");
}
} else if ("STRU".equals(command)) {
if ("F".equals(StringUtils.toUpperEnglish(param))) {
reply(200, "Ok");
} else {
reply(504, "Invalid");
}
}
break;
case 'T':
if ("TYPE".equals(command)) {
param = StringUtils.toUpperEnglish(param);
if ("A".equals(param) || "A N".equals(param)) {
reply(200, "Ok");
} else if ("I".equals(param) || "L 8".equals(param)) {
reply(200, "Ok");
} else {
reply(500, "Invalid");
}
}
break;
case 'X':
if ("XMKD".equals(command)) {
processMakeDir(param);
} else if ("XRMD".equals(command)) {
processRemoveDir(param);
}
break;
default:
break;
}
}
void processMakeDir(String param) throws IOException {
String fileName = getFileName(param);
boolean ok = false;
if (!readonly) {
try {
fs.mkdirs(fileName);
reply(257, StringUtils.quoteIdentifier(param) + " directory");
ok = true;
} catch (SQLException e) {
server.logError(e);
}
}
if (!ok) {
reply(500, "Failed");
}
}
void processRemoveDir(String param) throws IOException {
String fileName = getFileName(param);
if (!readonly && fs.exists(fileName) && fs.isDirectory(fileName) && fs.tryDelete(fileName)) {
reply(250, "Ok");
} else {
reply(500, "Failed");
}
}
private String getFileName(String file) {
return server.getFileName(file.startsWith("/") ? file : currentDir + file);
}
private String getPath(String path) {
return path.startsWith("/") ? path : currentDir + path;
}
private void processList(String param, boolean directories) throws SQLException, IOException {
String directory = getFileName(param);
if (!fs.exists(directory)) {
reply(450, "Directory does not exist");
return;
} else if (!fs.isDirectory(directory)) {
reply(450, "Not a directory");
return;
}
String list = server.getDirectoryListing(directory, directories);
reply(150, "Starting transfer");
server.log(list);
// need to use the current locale (UTF-8 would be wrong for the Windows
// Explorer)
data.send(list.getBytes());
reply(226, "Done");
}
private void reply(int code, String message) throws IOException {
server.log(code + " " + message);
output.print(code + " " + message + "\r\n");
output.flush();
replied = true;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?