ftpconnection.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 2,382 行 · 第 1/5 页
JAVA
2,382 行
String[] tmp = sortLs(); chdirNoRefresh(oldDir); if(tmp == null) { return GENERIC_FAILED; } for(int i = 0; i < tmp.length; i++) { Log.out("cleanDir: " + tmp); if(isSymlink(tmp[i])) { Log.debug("WARNING: Skipping symlink, remove failed."); Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); tmp[i] = null; } if(tmp[i] != null) { tmp[i] = parseSymlink(tmp[i]); } if((tmp[i] == null) || tmp[i].equals("./") || tmp[i].equals("../")) { //System.out.println(tmp[i]+"\n\n\n"); continue; } //System.out.println(tmp[i]); //pause(500); if(tmp[i].endsWith("/")) { // System.out.println(dir); cleanDir(dir + tmp[i], path); int x = removeFileOrDir(dir + tmp[i]); if(x < 0) { return x; } } else { // System.out.println(dir+tmp[i]); int x = removeFileOrDir(dir + tmp[i]); if(x < 0) { return x; } } } return REMOVE_SUCCESSFUL; } /** * Remove a remote file or directory. * * @param file The file to remove * @return REMOVE_SUCCESSFUL, REMOVE_FAILED, PERMISSION_DENIED or GENERIC_FAILED */ public int removeFileOrDir(String file) { if(file == null) { return 0; } if(file.trim().equals(".") || file.trim().equals("..")) { Log.debug("ERROR: Catching attempt to delete . or .. directories"); return GENERIC_FAILED; } if(isSymlink(file)) { Log.debug("WARNING: Skipping symlink, remove failed."); Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); return REMOVE_FAILED; } file = parseSymlink(file); if(file.endsWith("/")) { int ret = cleanDir(file, getCachedPWD()); if(ret < 0) { return ret; } jcon.send(RMD + " " + file); } else { jcon.send(DELE + " " + file); } if(success(POSITIVE))//FTP250_COMPLETED)) { return REMOVE_SUCCESSFUL; } else { return REMOVE_FAILED; } } /** * Disconnect from the server. * The connection is marked ad closed even if the server does not respond correctly - * if it fails for any reason the connection should just time out * */ public void disconnect() { jcon.send(QUIT); getLine(POSITIVE);//FTP221_SERVICE_CLOSING); connected = false; } /** * Execute a remote command. * Sends noops before and after the command * * @param cmd The raw command that is to be sent to the server */ public void sendRawCommand(String cmd) { noop(); Log.clearCache(); jcon.send(cmd); noop(); } /** * Reads the response until line found or error. * (Used internally) * * @param until The String the response line hast to start with, 2 for succesful return codes for example */ public String getLine(String until) { return getLine(new String[] { until }); } /** * Reads the response until line found or error. * (Used internally) */ public String getLine(String[] until) { BufferedReader in = null; try { in = jcon.getReader(); } catch(Exception ex) { Log.debug(ex.toString() + " @FtpConnection::getLine"); } //String resultString = null; String tmp; while(true) { try { tmp = in.readLine(); if(tmp == null) { break; } Log.debug(tmp); if(((tmp.startsWith(NEGATIVE) || (tmp.startsWith(NEGATIVE2))) && (tmp.charAt(3) != MORE_LINES_APPENDED)) || tmp.startsWith(PROCEED)) { return tmp; } else { for(int i = 0; i < until.length; i++) { if(tmp.startsWith(until[i])) { //if(resultString == null) resultString = tmp; //else resultString = resultString + "\n" + tmp; if(tmp.charAt(3) != MORE_LINES_APPENDED) { return tmp; } } } } } catch(Exception ex) { Log.debug(ex.toString() + " @FtpConnection::getLine"); break; } } return null; } /** * Check if login() was successful. * * @return True if connected, false otherwise */ public boolean isConnected() { return connected; } /** * Get the current remote directory. * * @return The server's CWD */ public String getPWD() { if(connected) { updatePWD(); } if(TESTMODE) { return "HUGO.user."; } return pwd; } /** * Returns current remote directory (cached) * * @return The cached CWD */ public String getCachedPWD() { return pwd; } /** * updates PWD */ private void updatePWD() { jcon.send(PWD); String tmp = getLine(POSITIVE);//FTP257_PATH_CREATED); if(tmp == null) { return; } if(TESTMODE) { tmp = "\"'T759F.'\""; } String x1 = tmp; if(x1.indexOf("\"") >= 0) { x1 = tmp.substring(tmp.indexOf("\"") + 1); x1 = x1.substring(0, x1.indexOf("\"")); } if(getOsType().indexOf("MVS") >= 0) { //if(x1.indexOf("'") == 0) { // String x2 = x1.substring(1, x1.lastIndexOf("'")); // x1 = x2; //} Log.out("pwd: " + x1); } else if(!x1.endsWith("/")) { x1 = x1 + "/"; } pwd = x1; } /** * Change directory unparsed. * Use chdir instead if you do not parse the dir correctly... * * @param dirName The raw directory name * @return True if successful, false otherwise */ public boolean chdirRaw(String dirName) { jcon.send(CWD + " " + dirName); return success(POSITIVE);//FTP250_COMPLETED); } private boolean success(String op) { String tmp = getLine(op); if((tmp != null) && tmp.startsWith(op)) { return true; } else { return false; } } /** * Change to the parent of the current working directory. * The CDUP command is a special case of CWD, and is included * to simplify the implementation of programs for transferring * directory trees between operating systems having different * syntaxes for naming the parent directory. * * @return True if successful, false otherwise */ public boolean cdup() { jcon.send(CDUP); return success(POSITIVE);//FTP200_OK); } /** * Create a directory with the given name. * * @param dirName The name of the directory to create * @return True if successful, false otherwise */ public boolean mkdir(String dirName) { jcon.send(MKD + " " + dirName); boolean ret = success(POSITIVE); // Filezille server bugfix, was: FTP257_PATH_CREATED); Log.out("mkdir(" + dirName + ") returned: " + ret); //*** Added 03/20/2005 fireDirectoryUpdate(this); //*** return ret; } private int negotiatePort() throws IOException { String tmp = ""; if(Settings.getFtpPasvMode()) { jcon.send(PASV); tmp = getLine(FTP227_ENTERING_PASSIVE_MODE); if((tmp != null) && !tmp.startsWith(NEGATIVE)) { return getPasvPort(tmp); } } tmp = getActivePortCmd(); jcon.send(tmp); getLine(FTP200_OK); return getActivePort(); } /** * List remote directory. * Note that you have to get the output using the * sort*-methods. * * @param outfile The file to save the output to, usually Settings.ls_out */ public void list() throws IOException { String oldType = ""; try { BufferedReader in = jcon.getReader(); int p = 0; modeStream(); oldType = getTypeNow(); ascii(); p = negotiatePort(); dcon = new DataConnection(this, p, host, null, DataConnection.GET, false, true); //,null); //System.out.println("2..."); while(dcon.getInputStream() == null) { //System.out.print("#"); pause(10); } DataInputStream input = new DataInputStream(dcon.getInputStream()); jcon.send(LIST); getLine(POSITIVE); //FTP226_CLOSING_DATA_REQUEST_SUCCESSFUL); String line; currentListing.removeAllElements(); while((line = input.readLine()) != null) { //System.out.println("-> "+line); if(!line.trim().equals("")) { currentListing.add(line); } } input.close(); //System.out.println("4..."); if(!oldType.equals(ASCII)) { type(oldType); } } catch(Exception ex) { Log.debug("Cannot list remote directory!"); if(!oldType.equals(ASCII)) { type(oldType); } ex.printStackTrace(); throw new IOException(ex.getMessage()); } } /** * Parses directory and does a chdir(). * Does also fire a directory update event. * * @return True is successful, false otherwise */ public boolean chdir(String p) { //Log.out("Change directory to: "+p); String tmpPath = p; boolean tmp = chdirWork(p); if(!tmp) { return f
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?