📄 commandhandler.java
字号:
data.closeDataServer = true;
handler.sendClientMsg("551 Error sending file : "+e);
}
} else {
handler.sendClientMsg("451 Sorry, that isn't a data file");
}
} else {
handler.sendClientMsg("425 Sorry no TCP connection was established.");
}
data.isTransferring = false;
} else if(ucCommand.startsWith("STOR")) {
//STORE
args = command.substring("STOR".length()).trim();
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("STOU")) {
//STORE UNIQUE - The 250 Transfer Started response
//must include the name generated.
args = command.substring("STOU".length()).trim();
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("APPE")) {
//APPEND (with create)
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("ALLO")) {
//ALLOCATE - obsolete
handler.sendClientMsg("502 Command not implemented - obsolete.");
} else if(ucCommand.startsWith("REST")) {
//RESTART transfer
//350 ok
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("RNFR")) {
//RENAME FROM
data.isRenameFrom = true;
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("RNTO")) {
//RENAME TO
if(!data.isRenameFrom) {
//error should not happen
}
data.isRenameFrom = false;
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("DELE")) {
//DELETE
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("RMD")) {
//REMOVE DIRECTORY
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("MKD")) {
//MAKE DIRECTORY
args = command.substring("MKD".length()).trim();
temp = MyString.replaceAll(data.root+data.wDir+args,"/","\\");
temp = MyString.replaceAll(temp,"\\\\","\\");
File file = new File(temp);
try {
file.mkdir();
file.canRead();
temp = file.getAbsolutePath();
temp = "/"+MyString.replaceAll(temp,data.root,"");
temp = MyString.replaceAll(temp,"\\","/");
temp = MyString.replaceAll(temp,"//","/");
handler.sendClientMsg("257 \""+temp+"\" directory created");
} catch(Exception e) {
handler.sendClientMsg("521-Could not create dir \""+args+"\"");
handler.sendClientMsg("521 Error : "+e);
}
} else if(ucCommand.startsWith("CWD")) {
//CHANGE DIRECTORY
temp = data.wDir;
args = command.substring("CWD".length()).trim();
if(data.wDir.charAt(data.wDir.length()-1)!='/')
data.wDir += "/";
if(args.charAt(args.length()-1)!='/')
args += "/";
if(args.charAt(0)!='/')
data.wDir += args;
else
data.wDir = args;
temp = MyString.replaceAll(data.root+data.wDir,"/","\\");
temp = MyString.replaceAll(temp,"\\\\","\\");
File file = new File(temp);
if(file.canRead() && file.isDirectory()) {
handler.sendClientMsg("250 Directory changed to "+data.wDir);
} else {
if(file.canRead())
handler.sendClientMsg("550 "+data.wDir+": The directory name is invalid.");
else
handler.sendClientMsg("550 "+data.wDir+": The system cannot find the file specified .");
data.wDir = temp;
logger.logp(Level.FINER, "CommandHandler", "handleCommand",
"Command=CWD; ERROR : 550 No such directory "+file);
}
} else if(ucCommand.startsWith("PWD")) {
//PRINT WORKING DIRECTORY
temp = MyString.replaceAll(data.wDir,"\"","\"\"");
handler.sendClientMsg("257 \""+data.wDir+"\"");
} else if(ucCommand.startsWith("LIST")) {
data.isTransferring = true;
if(ucCommand.equals("LIST")) {
args = "";
} else {
args = command.substring("LIST".length()).trim();
if(args.equals("-latr")) //not known
args="";
}
temp = MyString.replaceAll(data.root+data.wDir+args,"/","\\");
temp = MyString.replaceAll(temp,"\\\\","\\");
File file = new File(temp);
if( file.canRead() ) {
handler.sendClientMsg("150 Opening data connection for LIST "+data.wDir+args);
if(data.ip!=null) {
try {
data.socket = new Socket(
InetAddress.getByName(data.ip),
data.socketPort);
} catch (Exception e) {
handler.sendClientMsg("425 Can't open data connection.");
data.isTransferring = false;
return;
}
}
String result = winDirList(temp);
try {
data.sendData(result);
//close data connection when done
if(data.ip!=null) {
data.socket.close();
}
data.closeDataServer = true;
if(data.isStop==false)
handler.sendClientMsg("226 File transferred successfully.");
else
handler.sendClientMsg("551 Error sending file : User Aborted");
} catch (Exception e) {
if(data.ip!=null && data.socket!=null)
data.socket.close();
data.closeDataServer = true;
handler.sendClientMsg("551 Error sending LIST : "+e);
}
} else {
handler.sendClientMsg("550 No such directory : "+data.wDir+args);
logger.logp(Level.FINER, "CommandHandler", "handleCommand",
"Command=LIST; ERROR : 550 No such directory "+file);
}
data.isTransferring = false;
} else if(ucCommand.startsWith("NLST")) {
//NAME LIST of directory only
data.isTransferring = true;
if(ucCommand.equals("NLST")) {
args = "";
} else {
args = command.substring("NLST".length()).trim();
}
temp = MyString.replaceAll(data.root+data.wDir+args,"/","\\");
temp = MyString.replaceAll(temp,"\\\\","\\");
File file = new File(temp);
String result = "";
if( file.canRead() && file.isDirectory() ) {
handler.sendClientMsg("150 Opening data connection for LIST "+data.wDir+args);
if(data.ip!=null) {
try {
data.socket = new Socket(
InetAddress.getByName(data.ip),
data.socketPort);
} catch (Exception e) {
handler.sendClientMsg("425 Can't open data connection.");
data.isTransferring = false;
return;
}
}
String list[] = file.list();
for(int i=0;i<list.length;i++) {
if(!list[i].equals(".") && !list[i].equals("..") )
result +=list[i]+"\r\n";
}
try {
data.sendData(result);
//close data connection when done
if(data.ip!=null)
data.socket.close();
data.closeDataServer = true;
if(data.isStop==false)
handler.sendClientMsg("226 File transferred successfully.");
else
handler.sendClientMsg("551 Error sending file : User Aborted");
} catch (Exception e) {
if(data.ip!=null && data.socket!=null)
data.socket.close();
data.closeDataServer = true;
handler.sendClientMsg("551 Error sending NLST : "+e);
}
} else {
handler.sendClientMsg("550 No such directory : "+data.wDir+args);
logger.logp(Level.FINER, "CommandHandler", "handleCommand",
"Command=NLST; ERROR : 550 No such directory "+file);
}
data.isTransferring = false;
} else if(ucCommand.startsWith("SITE")) {
//SITE PARAMETERS
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("SYST")) {
//SYSTEM - Assigned Numbers document [4]
// UNIX Type: L8
handler.sendClientMsg("215 "+"Windows_NT version 5.0");
} else if(ucCommand.startsWith("HELP")) {
//HELP
//The reply is type 211 or 214.
handler.sendClientMsg("502 Command not implemented.");
} else if(ucCommand.startsWith("NOOP")) {
//NOOP - OK reply
handler.sendClientMsg("200 OK");
} else if(ucCommand.startsWith("SIZE")) {
//SIZE OF FILE
args = command.substring("SIZE".length()).trim();
temp = MyString.replaceAll(data.root+data.wDir+args,"/","\\");
temp = MyString.replaceAll(temp,"\\\\","\\");
File file = new File(temp);
if( file.canRead() ) {
handler.sendClientMsg("213 "+file.length());
} else {
handler.sendClientMsg("550 No such file.");
}
} else {
//ERROR
handler.sendClientMsg("500 Syntax error, command unrecognized.");
}
}
// helper meethods
private String dirList(String dir) {
//+FACTS1,FACTS@..,FACTN\tfile_name\r\n
/*
FACE = xy
>X<
r -> File
/ -> Dir
s ->Size, y=size in bytes
m ->Modified since 1970
i ->This file has identifier y.
up->Chmode is allowed
*/
File file = new File(dir);
File subFile = null;
String result = "";
if( file.canRead() ) {
String list[] = file.list();
for(int i=0;i<list.length;i++) {
//if(list[i].equals(".") || list[i].equals("..") )
// continue;
subFile = new File(dir+File.separator+list[i]);
result += "+";
result +="i"+subFile.hashCode()+",";
result +="s"+subFile.length()+",";
result +="m"+subFile.lastModified()+",";
if(subFile.isFile()) {
result +="r,";
} else {
result +="/,";
}
result +="\t"+list[i]+"\r\n";
}
}
return result;
}
private String winDirList(String dir) {
File file = new File(dir);
File subFile = null;
String result = "";
if( file.canRead() ) {
String list[] = file.list();
for(int i=0;i<list.length;i++) {
subFile = new File(dir+File.separator+list[i]);
SimpleDateFormat dformat =
new SimpleDateFormat("MM-dd-yy HH:mm:a ");
result += dformat.format(new Date(subFile.lastModified()));
if(subFile.isFile()) {
//20 field length
StringBuffer size = new StringBuffer(20);
size.append(subFile.length());
while(size.length()<20) {
size.insert(0," ");
}
result += size.toString();
} else {
result +=" <DIR> ";
}
result +=" "+list[i]+"\r\n";
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -