⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpconnection.java

📁 一个JAVA做的FTP软件,带源码的,可以很好的进行二次开发,,并带有详细说明文件的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    * @return The cached CWD    */    public String getCachedPWD()    {        return pwd;    }    /**    * updates PWD    */    private void updatePWD()    {        jcon.send(PWD);        String tmp = getLine(FTP257_PATH_CREATED);        String x1 = tmp.substring(tmp.indexOf("\"") + 1);        x1 = x1.substring(0, x1.indexOf("\""));        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(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(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.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(String outfile) throws IOException    {        String oldType = "";        try        {            BufferedReader in = jcon.getReader();            int p = 0;            modeStream();            oldType = getTypeNow();            ascii();            p = negotiatePort();            dcon = new DataConnection(this, p, host, outfile, DataConnection.GET); //,null);            //System.out.println("2...");            while(!dcon.isThere())            {                pause(10);            }            jcon.send(LIST);            //System.out.println("3...");            while(!dcon.finished)            {                pause(10);            }            getLine(FTP226_CLOSING_DATA_REQUEST_SUCCESSFUL);            //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)    {        boolean tmp = chdirWork(p);        if(!tmp)        {            return false;        }        else        {            fireDirectoryUpdate(this);            return true;        }    }    /**    * Parses directory and does a chdir(), but does not send an update signal    *    * @return True is successful, false otherwise    */    public boolean chdirNoRefresh(String p)    {        return chdirWork(p);    }    private boolean chdirWork(String p)    {        if(Settings.safeMode)        {            noop();        }        p = parseSymlinkBack(p);        if(getOsType().indexOf("OS/2") >= 0)        {            return chdirRaw(p);        }        try        {            // check if we don't have a relative path            if(!p.startsWith("/") && !p.startsWith("~"))            {                p = pwd + p;            }            // argh, we should document that!            if(p.endsWith(".."))            {                boolean home = p.startsWith("~");                StringTokenizer stok = new StringTokenizer(p, "/");                //System.out.println("path p :"+p +" Tokens: "+stok.countTokens());                if(stok.countTokens() > 2)                {                    String pold1 = "";                    String pold2 = "";                    String pnew = "";                    while(stok.hasMoreTokens())                    {                        pold1 = pold2;                        pold2 = pnew;                        pnew = pnew + "/" + stok.nextToken();                    }                    p = pold1;                }                else                {                    p = "/";                }                if(home)                {                    p = p.substring(1);                }            }            //System.out.println("path: "+p);            if(!chdirRaw(p))            {                return false;            }        }        catch(Exception ex)        {            Log.debug("(remote) Can not get pathname! " + pwd + ":" + p);            ex.printStackTrace();            return false;        }        //fireDirectoryUpdate(this);        updatePWD();        return true;    }    /**    * Waits a specified amount of time    *    * @param time The time to sleep in milliseconds    */    public void pause(int time)    {        try        {            Thread.sleep(time);        }        catch(Exception ex)        {            ex.printStackTrace();        }    }    /**    * Return the local working directory    *    * @return The local CWD    */    public String getLocalPath()    {        return localPath;    }    /**    * Set the local working directory    *    * @param newPath The new local CWD    * @return Always true in the current implementation    */    public boolean setLocalPath(String newPath)    {        localPath = newPath;        if(!localPath.endsWith("/"))        {            localPath = localPath + "/";        }        return true;    }    /**    * Checks wheter a file exists.    *    * @param file the name of the file    * @return An int-code: CHDIR_FAILED, PERMISSION_DENIED (no listing allowed), R, W or DENIED    */    public int exists(String file)    {        String dir = null;        String tmpPWD = getCachedPWD();        if(file.indexOf("/") >= 0)        {            dir = file.substring(0, file.lastIndexOf("/") + 1);            Log.out("checking dir: " + dir);            if(!chdir(dir))            {                return CHDIR_FAILED;            }        }        try        {            list(Settings.ls_out);        }        catch(IOException ex)        {            ex.printStackTrace();            return PERMISSION_DENIED;        }        String f = file.substring(file.lastIndexOf("/") + 1);        Log.out("checking file: " + f);        String[] files = sortLs(Settings.ls_out);        int[] perms = getPermissions(Settings.ls_out);        int y = -1;        for(int x = 0; x < files.length; x++)        {            if(files[x].equals(f))            {                y = x;                break;            }        }        if(y == -1)        {            return FILE_NOT_FOUND;        }        if(dir != null)        {            chdir(tmpPWD);        }        return perms[y];    }    /** Moves or renames remote file.     * @param from remote file to move or rename.     * @param to new file name.     * @return <code>true</code> if completed successfully; <code>false</code> otherwise.     */    public boolean rename(String from, String to)    {        jcon.send("RNFR " + from);        if(success(RC350))        {            jcon.send("RNTO " + to);            if(success(FTP250_COMPLETED))            {                return true;            }        }        return false;    }    /**    * Get the port.    *    * @return The port used by the FTP control connection    */    public int getPort()    {        return port;    }    private int getPortA()    {        return porta;    }    private int getPortB()    {        return portb;    }    private void incrementPort()    {        portb++;    }    private String getActivePortCmd() throws UnknownHostException, IOException    {        InetAddress ipaddr = jcon.getLocalAddress();        String ip = ipaddr.getHostAddress().replace('.', ',');        incrementPort();        int a = getPortA();        int b = getPortB();        String ret = "PORT " + ip + "," + a + "," + b;        //System.out.println(ret);        return ret;    }    /**     * Tell server we want binary data connections.     */    public void binary()    { // possible responses 200, 500, 501, 504, 421 and 530        type(BINARY);    }    /**     * Tell server we want ascii data connections.     */    public void ascii()    {        type(ASCII);    }    /**    * Set type (L8,I,A for example).    *    * @return True if the type was changed successfully, false otherwise    */    public boolean type(String code)    {        jcon.send(TYPE + " " + code);        if(getLine(FTP200_OK).startsWith(FTP200_OK))        {            typeNow = code;            return true;        }        return false;    }    /**    * Returns the type used.    *    * @return The type String, I, A, L8 for example    */    public String getTypeNow()    {        return typeNow;    }    /**     * Do nothing, but flush buffers     */    public void noop()    {        jcon.send(NOOP);        getLine(FTP200_OK);    }    /**     * Try to abort the transfer.     */    public void abort()    {        jcon.send(ABOR);        getLine(POSITIVE); // 226    }    /**     * This command is used to find out the type

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -