📄 ftpserver.java
字号:
} case CMD_SYST: reply(215, eventHandler.system()); break; case CMD_NOOP: reply(200, "NOOP command successful."); break; case CMD_FEAT: reply(211, "Extensions supported:\nSIZE\nMDTM\nEND"); break; case CMD_MDTM: checkArgAndPlainFile(argv); long modt = eventHandler.modTime(argv[1]); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss"); reply(213, df.format(new Date(modt))); break; case CMD_SIZE: checkArgAndPlainFile(argv); long size = eventHandler.size(argv[1]); reply(213, String.valueOf(size)); break; case CMD_PASS: /* * Allow buggy clients to send PASS eventhough they are * logged in (e.g. dreamweaver it seems) */ reply(230, "User " + user + " logged in."); break; case CMD_HELP: case CMD_STAT: case CMD_SITE: case CMD_USER: case CMD_ACCT: case CMD_SMNT: case CMD_REIN: case CMD_STRU: case CMD_MODE: case CMD_STOU: case CMD_APPE: case CMD_ALLO: case CMD_REST: reply(502, "'" + argv[0] + "': command not implemented"); break; case CMD_UNKN: reply(500, "'" + argv[0] + "': command not understood"); break; } } catch (FTPException e) { if(dataConnected) { dataReset(); } reply(e.getCode(), e.getMessage()); } } } catch (IOException e) { // !!! TODO eventhandler.error ... } finally { try { cmdOutput.close(); } catch (IOException e) { /* don't care */ } try { cmdInput.close(); } catch (IOException e) { /* don't care */ } // !!! TODO other clean up ??? } } public void terminate() { try { cmdOutput.close(); } catch (IOException e) { /* don't care */ } try { cmdInput.close(); } catch (IOException e) { /* don't care */ } } private void checkArgAndPlainFile(String[] argv) throws FTPException { if(argv.length < 2 || argv[1] == null) { throw new FTPException(500, "'" + argv[0] + "': command not understood"); } if(!eventHandler.isPlainFile(argv[1])) { throw new FTPException(550, argv[1] + ": Not a plain file."); } } private int readCommand(String[] argv) throws IOException { String cmdLine = cmdInput.readLine(); if(cmdLine == null) { // !!! TODO throw new EOFException(); } int i = cmdLine.indexOf(' '); if(i != -1) { argv[0] = cmdLine.substring(0, i); argv[1] = cmdLine.substring(i + 1); } else { argv[0] = cmdLine.trim(); argv[1] = null; } argv[0] = argv[0].toUpperCase(); Integer code = (Integer)commands.get(argv[0]); if(DEBUG) System.out.println("ftp cmd: " + argv[0] + " " + argv[1]); if(code == null) { return -1; } return code.intValue(); } public void reply(int code, String text) throws IOException { String msg; if(DEBUG) System.out.println("ftp reply: " + code + " " + text); if(text.indexOf('\n') != -1) { msg = code + "-"; StringTokenizer st = new StringTokenizer(text, "\r\n"); while(st.hasMoreTokens()) { String line = st.nextToken(); if(st.hasMoreTokens()) { msg += " " + line + "\r\n"; } else { msg += code + " " + line + "\r\n"; } } } else { msg = code + " " + text + "\r\n"; } cmdOutput.write(msg.getBytes()); } public void readLogin() throws IOException { user = null; password = null; while(user == null || password == null) { String[] argv = new String[2]; int code = readCommand(argv); if(code == CMD_USER) { user = argv[1]; if(user != null) { if(needPassword) { reply(331, "Password required for " + user + "."); continue; } else { return; } } } else if(code == CMD_PASS && user != null) { password = argv[1]; } if(password == null) { reply(530, "Please login with USER and PASS."); } } } protected int[] dataPort(String arg) throws FTPException { dataReset(); StringTokenizer st = new StringTokenizer(arg, ","); int i = 0; int[] d = new int[6]; while(i < 6 && st.hasMoreTokens()) { try { d[i++] = Integer.parseInt(st.nextToken()); } catch (NumberFormatException e) { throw new FTPException(500, "'PORT " + arg + "': command not understood"); } } if(i < 6) { throw new FTPException(500, "'PORT " + arg + "': command not understood"); } return d; } protected String dataPassive() throws FTPException { dataReset(); try { dataPasvAccept = new ServerSocket(0, 16, localHost); dataPasvAccept.setSoTimeout(30000); } catch (Exception e) { dataReset(); throw new FTPException(500, "Error, unable to proceede: " + e.getMessage()); } byte[] localAddrArr = null; localAddrArr = localHost.getAddress(); // !!! TODO: want to be able to get actual connected adress if((localAddrArr[0] & 0xff) == 0) { try { localAddrArr = InetAddress.getLocalHost().getAddress(); } catch (UnknownHostException e) { throw new Error("Error in FTPServer: " + e); } } int a1 = localAddrArr[0] & 0xff; int a2 = localAddrArr[1] & 0xff; int a3 = localAddrArr[2] & 0xff; int a4 = localAddrArr[3] & 0xff; int port = dataPasvAccept.getLocalPort(); int p1 = (port >>> 8) & 0xff; int p2 = port & 0xff; return a1 + "," + a2 + "," + a3 + "," + a4 + "," + p1 + "," + p2; } protected OutputStream getDataOutput() throws FTPException { dataConnect(); if(dataSocket == null) { throw new FTPException(550, "Error when making data connection."); } try { dataOut = dataSocket.getOutputStream(); return dataOut; } catch (IOException e) { throw new FTPException(425, "Can't build data connection: " + e.getMessage()); } } protected InputStream getDataInput() throws FTPException { dataConnect(); try { dataIn = dataSocket.getInputStream(); return dataIn; } catch (IOException e) { throw new FTPException(425, "Can't build data connection: " + e.getMessage()); } } protected void dataConnect() throws FTPException { try { if(dataPasvAccept != null) { dataSocket = dataPasvAccept.accept(); } else if(dataPortAddr != null) { String toHost = dataPortAddr[0] + "." + dataPortAddr[1] + "." + dataPortAddr[2] + "." + dataPortAddr[3]; int toPort = (dataPortAddr[4] << 8) | dataPortAddr[5]; dataSocket = new Socket(toHost, toPort); } dataConnected = true; } catch (IOException e) { dataReset(); throw new FTPException(425, "Can't build data connection: " + e.getMessage()); } } protected void dataStart(String obj) throws IOException { reply(150, "Opening " + (type.equals("A") ? "ASCII" : "BINARY") + " mode data connection for " + obj + "."); } protected void dataComplete() throws IOException { reply(226, "Transfer complete."); dataReset(); } protected void dataReset() { if(dataPasvAccept != null) { try { dataPasvAccept.close(); } catch (Exception e) { /* don't care */ } dataPasvAccept = null; } dataPortAddr = null; if(dataSocket != null) { try { if(dataOut != null) dataOut.close(); } catch (IOException e) { /* don't care */ } try { if(dataIn != null) dataIn.close(); } catch (IOException e) { /* don't care */ } try { dataSocket.close(); } catch (Exception e) { /* don't care */ } } dataSocket = null; dataOut = null; dataIn = null; dataConnected = false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -