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

📄 ftpconnection.java

📁 一个JAVA做的FTP软件,带源码的,可以很好的进行二次开发,,并带有详细说明文件的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    private int getActivePort()    {        return (getPortA() * 256) + getPortB();    }    /** parse a symlink */    private String parseSymlink(String file)    {        if(file.indexOf("->") >= 0)        {            //System.out.print(file+" :-> symlink converted to:");            file = file.substring(0, file.indexOf("->")).trim();            file = file + "###";            //System.out.println(file);        }        return file;    }    /** parse a symlink and cut the back */    private String parseSymlinkBack(String file)    {        if(file.indexOf("->") >= 0)        {            //System.out.print(file+" :-> symlink converted to:");            file = file.substring(file.indexOf("->") + 2).trim();            //System.out.println(file);        }        return file;    }    /** test for symlink */    private boolean isSymlink(String file)    {        if(file.indexOf("->") >= 0)        {            return true;        }        return false;    }    /** Download a file or directory.    * Uses multithreading if enabled and does not block.    *    * @param file The file to download    * @return An int-statuscode, see constants defined in this class for details    */    public int handleDownload(String file)    {        if(Settings.getEnableMultiThreading())        {            Log.out("spawning new thread for this download.");            FtpTransfer t = new FtpTransfer(host, port, getLocalPath(),                                            getCachedPWD(), file, username,                                            password, Transfer.DOWNLOAD,                                            handler, listeners);            lastTransfer = t;            return NEW_TRANSFER_SPAWNED;        }        else        {            Log.out("multithreading is completely disabled.");            return download(file);        }    }    /**    * Download a file or directory, block until finished.    *    * @param file The file to download    * @return An int returncode    */    public int download(String file)    {        Log.out("ftp download started:" + this);        int stat;        if(file.endsWith("/"))        {            shortProgress = true;            fileCount = 0;            baseFile = file;            dataType = DataConnection.GETDIR;            stat = downloadDir(file);            pause(500);            fireActionFinished(this);            fireProgressUpdate(baseFile,                               DataConnection.DFINISHED + ":" + fileCount, -1);            shortProgress = false;        }        else        {            dataType = DataConnection.GET;            stat = rawDownload(file);	            try        {            Thread.sleep(100);        }        catch(Exception ex)        {        }            fireActionFinished(this);        }        try        {            Thread.sleep(400);        }        catch(Exception ex)        {        }        return stat;    }    /**    * Get download InputStream.    *    * @param file The file to download    * @return An InputStream    */    public InputStream getDownloadInputStream(String file)    {        Log.out("ftp stream download started:" + this);        file = parse(file);        try        {            int p = 0;            dataType = DataConnection.GET;            file = StringUtils.getFile(file);            String path = getLocalPath() + file;            BufferedReader in = jcon.getReader();            modeStream();            p = negotiatePort();            dcon = new DataConnection(this, p, host, path, dataType, false, true);            while(!dcon.isThere())            {                pause(10);            }            jcon.send(RETR + " " + file);            return dcon.getInputStream();        }        catch(Exception ex)        {            ex.printStackTrace();            Log.debug(ex.toString() +                      " @FtpConnection::getDownloadInputStream");            return null;        }    }    private int rawDownload(String file)    {        file = parse(file);        //String path = file;        try        {            int p = 0;            //if(isRelative(file)) path = getLocalPath() + file;            file = StringUtils.getFile(file);            String path = getLocalPath() + file;            //System.out.println(file + " : " + path);            //BufferedReader in = jcon.getReader();            modeStream();            //binary();            p = negotiatePort();            File f = new File(path);            //System.out.println("path: "+path);            boolean resume = false;            if(f.exists() && Settings.enableResuming)            {                jcon.send(REST + " " + f.length());                if(getLine(PROCEED) != null)                {                    resume = true;                }            }            dcon = new DataConnection(this, p, host, path, dataType, resume); //, new Updater());            while(!dcon.isThere())            {                pause(10);            }            jcon.send(RETR + " " + file);            String line = getLine(POSITIVE);            Log.debug(line);            // file has been created even if not downloaded, delete it            if(line.startsWith(NEGATIVE))            {                File f2 = new File(path);                if(f2.exists() && (f2.length() == 0))                {                    f2.delete();                }                return PERMISSION_DENIED;            }            else if(!line.startsWith(POSITIVE) && !line.startsWith(PROCEED))            {                return TRANSFER_FAILED;            }            // 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::download");            return TRANSFER_FAILED;        }        return TRANSFER_SUCCESSFUL;    }    private int downloadDir(String dir)    {        if(!dir.endsWith("/"))        {            dir = dir + "/";        }        if(StringUtils.isRelative(dir))        {            dir = getCachedPWD() + dir;        }        String path = getLocalPath() + StringUtils.getDir(dir);        //System.out.println("path: "+path);        String pwd = getCachedPWD() + StringUtils.getDir(dir);        String oldDir = getLocalPath();        String oldPwd = getCachedPWD();        File f = new File(path);        if(!f.exists())        {            if(!f.mkdir())            {                Log.debug("Can't create directory: " + dir);            }            else            {                Log.debug("Created directory...");            }        }        setLocalPath(path);        if(!chdirNoRefresh(pwd))        {            return CHDIR_FAILED;        }        try        {            list(Settings.ls_out);        }        catch(IOException ex)        {            return PERMISSION_DENIED;        }        String[] tmp = sortLs(Settings.ls_out);        setLocalPath(path);        for(int i = 0; i < tmp.length; i++)        {            if(tmp[i].endsWith("/"))            {                if(tmp[i].trim().equals("../") || tmp[i].trim().equals("./"))                {                    Log.debug("Skipping " + tmp[i].trim());                }                else                {                    if(!work)                    {                        return TRANSFER_STOPPED;                    }                    if(downloadDir(tmp[i]) < 0)                    {                        ; // return TRANSFER_FAILED;                    }                }            }            else            {                //System.out.println( "file: " + getLocalPath() + tmp[i] + "\n\n");                if(!work)                {                    return TRANSFER_STOPPED;                }                fileCount++;                if(rawDownload(getLocalPath() + tmp[i]) < 0)                {                    ; // return TRANSFER_FAILED;                }            }        }        chdirNoRefresh(oldPwd);        setLocalPath(oldDir);        return TRANSFER_SUCCESSFUL;    }    /** Upload a file or directory.    * Uses multithreading if enabled and does not block.    *    * @param file The file to upload    * @return An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL    */    public int handleUpload(String file)    {        return handleUpload(file, null);    }    /** Upload a file or directory.    * Uses multithreading if enabled and does not block.    *    * @param file The file to upload    * @param realName The file to rename the uploaded file to    * @return An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL    */    public int handleUpload(String file, String realName)    {        if(Settings.getEnableMultiThreading() &&               (!Settings.getNoUploadMultiThreading()))        {            Log.out("spawning new thread for this upload.");            FtpTransfer t;            if(realName != null)            {                t = new FtpTransfer(host, port, getLocalPath(), getCachedPWD(),                                    file, username, password, Transfer.UPLOAD,                                    handler, listeners, realName);            }            else            {                t = new FtpTransfer(host, port, getLocalPath(), getCachedPWD(),                                    file, username, password, Transfer.UPLOAD,                                    handler, listeners);            }            lastTransfer = t;            return NEW_TRANSFER_SPAWNED;        }        else        {            if(Settings.getNoUploadMultiThreading())            {                Log.out("upload multithreading is disabled.");            }            else            {                Log.out("multithreading is completely disabled.");            }            if(realName == null)            {                return upload(file);            }            else            {                return upload(file, realName);            }        }    }    /**    * Upload a file or directory, block until finished.    *    * @param file The file to upload    * @return An int returncode    */    public int upload(String file)    {        return upload(file, file);    }    /**    * Upload and a file or directory under a given name, block until finished.    * Note that setting realName does not affect directory transfers    *    * @param file The file to upload    * @param realName The file to rename the uploaded file to    * @return An int responsecode    */    public int upload(String file, String realName)    {        return upload(file, realName, null);    }    /**    * Upload from an InputStream, block until finished.    *    * @param file The file to upload    * @param in InputStream to read from    * @return An int responsecode    */    public int upload(String file, InputStream in)    {        return upload(file, file, in);    }    /**    * Upload and a file or directory under a given name, block until finished.    * Note that setting realName does not affect directory transfers    *    * @param file The file to upload    * @param realName The file to rename the uploaded file to    * @param in InputStream to read from    * @return An int responsecode    */    public int upload(String file, String realName, InputStream in)    {        hasUploaded = true;        Log.out("ftp upload started: " + this);        int stat;        if((in == null) && new File(file).isDirectory())        {            shortProgress = true;            fileCount = 0;            baseFile = file;            dataType = DataConnection.PUTDIR;            isDirUpload = true;            stat = uploadDir(file);            shortProgress = false;            //System.out.println(fileCount + ":" + baseFile);            fireProgressUpdate(baseFile,                               DataConnection.DFINISHED + ":" + fileCount, -1);            fireActionFinished(this);            fireDirectoryUpdate(this);        }        else        {            dataType = DataConnection.PUT;            stat = rawUpload(file, realName, in);	    	try        {            Thread.sleep(100);        }        catch(Exception ex)        {        }	            fireActionFinished(this);            fireDirectoryUpdate(this);        }        try        {            Thread.sleep(500);        }        catch(Exception ex)        {        }        return stat;    }

⌨️ 快捷键说明

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