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

📄 ftpconnection.java

📁 一个JAVA做的FTP软件,带源码的,可以很好的进行二次开发,,并带有详细说明文件的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    private int rawUpload(String file)    {        return rawUpload(file, file);    }    private int rawUpload(String file, String realName)    {        return rawUpload(file, realName, null);    }    /** uploads a file */    private int rawUpload(String file, String realName, InputStream in)    {        if(!file.equals(realName))        {            Log.debug("File: " + file + ", stored as " + realName);        }        else        {            Log.debug("File: " + file);            realName = null;        }        file = parse(file);        String path = file;        try        {            int p = 0;            if(StringUtils.isRelative(file))            {                path = getLocalPath() + file;            }            file = StringUtils.getFile(file);            //BufferedReader in = jcon.getReader();            boolean resume = false;            String size = "0";            if(Settings.enableUploadResuming && (in == null))            {                list(Settings.ls_out);                String[] ls = sortLs(Settings.ls_out);                String[] sizes = sortSize(Settings.ls_out);                if((ls == null) || (sizes == null))                {                    Log.out(">>> ls out of sync (skipping resume check)");                }                else                {                    for(int i = 0; i < ls.length; i++)                    {                        //Log.out(ls[i] + ":" + sizes[i]);                        if(realName != null)                        {                            if(ls[i].equals(realName))                            {                                resume = true;                                size = sizes[i];                                break;                            }                        }                        else                        {                            if(ls[i].equals(file))                            {                                resume = true;                                size = sizes[i];                            }                        }                    }                    File f = new File(path);                    if(f.exists() && (f.length() <= Integer.parseInt(size)))                    {                        Log.out("skipping resuming, file is <= filelength");                        resume = false;                    }                    else                    {                        Log.out("resume: " + resume + ", size: " + size);                    }                }            }            modeStream();            //binary();            p = negotiatePort();            if(resume && Settings.enableUploadResuming)            {                jcon.send(REST + " " + size);                if(getLine(PROCEED) == null)                {                    resume = false;                }            }            dcon = new DataConnection(this, p, host, path, dataType, resume,                                      Integer.parseInt(size), in); //, new Updater());            while(!dcon.isThere())            {                pause(10);            }            //System.out.println(path + " : " + file);            if(realName != null)            {                jcon.send(STOR + " " + realName);            }            else            {                jcon.send(STOR + " " + file);            }            String tmp = getLine(POSITIVE);            if(!tmp.startsWith(POSITIVE) && !tmp.startsWith(PROCEED))            {                return TRANSFER_FAILED;            }            Log.debug(tmp);            // we need to block since some ftp-servers do not want the            // refresh command that dirpanel sends otherwise            while(!dcon.finished)            {                pause(10);            }        }        catch(Exception ex)        {            ex.printStackTrace();            Log.debug(ex.toString() + " @FtpConnection::upload");            return TRANSFER_FAILED;        }        return TRANSFER_SUCCESSFUL;    }    private int uploadDir(String dir)    {        //System.out.println("up");        if(dir.endsWith("\\"))        {            System.out.println("Something's wrong with the selected directory - please report this bug!");        }        if(!dir.endsWith("/"))        {            dir = dir + "/";        }        if(StringUtils.isRelative(dir))        {            dir = getLocalPath() + dir;        }        String single = StringUtils.getDir(dir);        String path = dir.substring(0, dir.indexOf(single));        String remoteDir = getCachedPWD() + single; //StringUtils.removeStart(dir,path);        String oldDir = getCachedPWD();        if(Settings.safeMode)        {            noop();        }        boolean successful = mkdir(remoteDir);        if(!successful)        {            return MKDIR_FAILED;        }        if(Settings.safeMode)        {            noop();        }        chdirNoRefresh(remoteDir);        File f2 = new File(dir);        String[] tmp = f2.list();        for(int i = 0; i < tmp.length; i++)        {            String res = dir + tmp[i];            File f3 = new File(res);            if(f3.isDirectory())            {                if(!work)                {                    return TRANSFER_STOPPED;                }                uploadDir(res);            }            else            {                //System.out.println(">>>\nlp: "+path+single + "\nrp: ");                //System.out.println(remoteDir +"\nres: "+res);                if(!work)                {                    return TRANSFER_STOPPED;                }                fileCount++;                if(rawUpload(res) < 0)                {                    ; //return TRANSFER_STOPPED;                }            }        }        chdirNoRefresh(oldDir);        return TRANSFER_SUCCESSFUL;    }    private String parse(String file)    {        file = parseSymlink(file);        return file;    }    /** recursive delete remote directory */    private int cleanDir(String dir, String path)    {        if(dir.equals(""))        {            return 0;        }        if(!dir.endsWith("/"))        {            dir = dir + "/";        }        String remoteDir = StringUtils.removeStart(dir, path);        String oldDir = pwd;        chdirNoRefresh(pwd + remoteDir);        try        {            list(Settings.ls_out);        }        catch(IOException ex)        {            // probably we don't have permission to ls here            return PERMISSION_DENIED;        }        String[] tmp = sortLs(Settings.ls_out);        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(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(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();        }        return pwd;    }    /**    * Returns current remote directory (cached)    *

⌨️ 快捷键说明

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