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

📄 nfsconnection.java

📁 一个JAVA做的FTP软件,带源码的,可以很好的进行二次开发,,并带有详细说明文件的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            {                accessible = FtpConnection.R;            }            else            {                accessible = FtpConnection.DENIED;            }            perms[i] = accessible;            //System.out.println(pwd+files[i] +" : " +accessible + " : " + size[i]);        }        return files;    }    public String[] sortSize(String file)    {        return size;    }    public int[] getPermissions(String file)    {        return perms;    }    public int handleUpload(String f)    {        upload(f);        return 0;    }    public int handleDownload(String f)    {        download(f);        return 0;    }    public int upload(String f)    {        String file = toNFS(f);        if(file.endsWith("/"))        {            String out = StringUtils.getDir(file);            uploadDir(file, getLocalPath() + out);            fireActionFinished(this);        }        else        {            String outfile = StringUtils.getFile(file);            //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);            work(getLocalPath() + outfile, file);            fireActionFinished(this);        }        return 0;    }    public int download(String f)    {        String file = toNFS(f);        if(file.endsWith("/"))        {            String out = StringUtils.getDir(file);            downloadDir(file, getLocalPath() + out);            fireActionFinished(this);        }        else        {            String outfile = StringUtils.getFile(file);            //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);            work(file, getLocalPath() + outfile);            fireActionFinished(this);        }        return 0;    }    private void downloadDir(String dir, String out)    {        try        {            //System.out.println("downloadDir: " + dir + "," + out);            fileCount = 0;            shortProgress = true;            baseFile = StringUtils.getDir(dir);            XFile f2 = new XFile(dir);            String[] tmp = f2.list();            if(tmp == null)            {                return;            }            File fx = new File(out);            fx.mkdir();            for(int i = 0; i < tmp.length; i++)            {                tmp[i] = tmp[i].replace('\\', '/');                //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);                XFile f3 = new XFile(dir + tmp[i]);                if(f3.isDirectory())                {                    if(!tmp[i].endsWith("/"))                    {                        tmp[i] = tmp[i] + "/";                    }                    downloadDir(dir + tmp[i], out + tmp[i]);                }                else                {                    fileCount++;                    fireProgressUpdate(baseFile,                                       DataConnection.GETDIR + ":" + fileCount,                                       -1);                    work(dir + tmp[i], out + tmp[i]);                }            }            fireProgressUpdate(baseFile,                               DataConnection.DFINISHED + ":" + fileCount, -1);        }        catch(Exception ex)        {            ex.printStackTrace();            //System.out.println(dir + ", " + out);            Log.debug("Transfer error: " + ex);            fireProgressUpdate(baseFile,                               DataConnection.FAILED + ":" + fileCount, -1);        }        shortProgress = false;    }    private void uploadDir(String dir, String out)    {        try        {            //System.out.println("uploadDir: " + dir + "," + out);            isDirUpload = true;            fileCount = 0;            shortProgress = true;            baseFile = StringUtils.getDir(dir);            File f2 = new File(out);            String[] tmp = f2.list();            if(tmp == null)            {                return;            }            XFile fx = new XFile(dir);            fx.mkdir();            for(int i = 0; i < tmp.length; i++)            {                tmp[i] = tmp[i].replace('\\', '/');                //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);                File f3 = new File(out + tmp[i]);                if(f3.isDirectory())                {                    if(!tmp[i].endsWith("/"))                    {                        tmp[i] = tmp[i] + "/";                    }                    uploadDir(dir + tmp[i], out + tmp[i]);                }                else                {                    fileCount++;                    fireProgressUpdate(baseFile,                                       DataConnection.PUTDIR + ":" + fileCount,                                       -1);                    work(out + tmp[i], dir + tmp[i]);                }            }            fireProgressUpdate(baseFile,                               DataConnection.DFINISHED + ":" + fileCount, -1);        }        catch(Exception ex)        {            ex.printStackTrace();            //System.out.println(dir + ", " + out);            Log.debug("Transfer error: " + ex);            fireProgressUpdate(baseFile,                               DataConnection.FAILED + ":" + fileCount, -1);        }        isDirUpload = false;        shortProgress = true;    }    private void work(String file, String outfile)    {        try        {            BufferedOutputStream out = null;            boolean outflag = false;            if(outfile.startsWith("nfs://"))            {                outflag = true;                out = new BufferedOutputStream(new XFileOutputStream(outfile));            }            else            {                out = new BufferedOutputStream(new FileOutputStream(outfile));            }            //System.out.println("out: " + outfile + ", in: " + file);            BufferedInputStream in = null;            if(file.startsWith("nfs://"))            {                in = new BufferedInputStream(new XFileInputStream(file));            }            else            {                in = new BufferedInputStream(new FileInputStream(file));            }            byte[] buf = new byte[buffer];            int len = 0;            int reallen = 0;            //System.out.println(file+":"+getLocalPath()+outfile);            while(true)            {                len = in.read(buf);                //System.out.print(".");                if(len == StreamTokenizer.TT_EOF)                {                    break;                }                out.write(buf, 0, len);                reallen += len;                //System.out.println(file + ":" + StringUtils.getFile(file));                if(outflag)                {                    fireProgressUpdate(StringUtils.getFile(outfile),                                       DataConnection.PUT, reallen);                }                else                {                    fireProgressUpdate(StringUtils.getFile(file),                                       DataConnection.GET, reallen);                }            }            out.flush();            out.close();            in.close();            fireProgressUpdate(file, DataConnection.FINISHED, -1);        }        catch(IOException ex)        {            Log.debug("Error with file IO (" + ex + ")!");            fireProgressUpdate(file, DataConnection.FAILED, -1);        }    }    private void update(String file, String type, int bytes)    {        if(listeners == null)        {            return;        }        else        {            for(int i = 0; i < listeners.size(); i++)            {                ConnectionListener listener = (ConnectionListener) listeners.elementAt(i);                listener.updateProgress(file, type, bytes);            }        }    }    public void addConnectionListener(ConnectionListener l)    {        listeners.add(l);    }    public void setConnectionListeners(Vector l)    {        listeners = l;    }    /** remote directory has changed */    public void fireDirectoryUpdate()    {        if(listeners == null)        {            return;        }        else        {            for(int i = 0; i < listeners.size(); i++)            {                ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this);            }        }    }    /** progress update */    public void fireProgressUpdate(String file, String type, int bytes)    {        //System.out.println(listener);        if(listeners == null)        {            return;        }        else        {            for(int i = 0; i < listeners.size(); i++)            {                ConnectionListener listener = (ConnectionListener) listeners.elementAt(i);                if(shortProgress && Settings.shortProgress)                {                    if(type.startsWith(DataConnection.DFINISHED))                    {                        listener.updateProgress(baseFile,                                                DataConnection.DFINISHED + ":" +                                                fileCount, bytes);                    }                    else if(isDirUpload)                    {                        listener.updateProgress(baseFile,                                                DataConnection.PUTDIR + ":" +                                                fileCount, bytes);                    }                    else                    {                        listener.updateProgress(baseFile,                                                DataConnection.GETDIR + ":" +                                                fileCount, bytes);                    }                }                else                {                    listener.updateProgress(file, type, bytes);                }            }        }    }    public void fireActionFinished(NfsConnection con)    {        if(listeners == null)        {            return;        }        else        {            for(int i = 0; i < listeners.size(); i++)            {                ((ConnectionListener) listeners.elementAt(i)).actionFinished(con);            }        }    }    public int upload(String file, InputStream i)    {        try        {            file = toNFS(file);            BufferedOutputStream out = new BufferedOutputStream(new XFileOutputStream(file));            BufferedInputStream in = new BufferedInputStream(i);            byte[] buf = new byte[buffer];            int len = 0;            int reallen = 0;            while(true)            {                len = in.read(buf);                if(len == StreamTokenizer.TT_EOF)                {                    break;                }                out.write(buf, 0, len);                reallen += len;                fireProgressUpdate(StringUtils.getFile(file),                                   DataConnection.PUT, reallen);            }            out.flush();            out.close();            in.close();            fireProgressUpdate(file, DataConnection.FINISHED, -1);        }        catch(IOException ex)        {            Log.debug("Error with file IO (" + ex + ")!");            fireProgressUpdate(file, DataConnection.FAILED, -1);            return -1;        }        return 0;    }    public InputStream getDownloadInputStream(String file)    {        file = toNFS(file);        Log.debug(file);        try        {            return new BufferedInputStream(new XFileInputStream(file));        }        catch(Exception ex)        {            ex.printStackTrace();            Log.debug(ex.toString() +                      " @NfsConnection::getDownloadInputStream");            return null;        }    }}

⌨️ 快捷键说明

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