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

📄 dataconnection.java

📁 一个JAVA做的FTP软件,带源码的,可以很好的进行二次开发,,并带有详细说明文件的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package net.sf.jftp.net;import net.sf.jftp.config.Settings;import net.sf.jftp.util.Log;import java.io.*;import java.net.*;import java.util.*;/*** This class represents a ftp data connection.* It is used internally by FtpConnection, so you probably don't have to use it directly.*/public class DataConnection implements Runnable{    public final static String GET = "GET";    public final static String PUT = "PUT";    public final static String FAILED = "FAILED";    public final static String FINISHED = "FINISHED";    public final static String DFINISHED = "DFINISHED";    public final static String GETDIR = "DGET";    public final static String PUTDIR = "DPUT";    private BufferedInputStream in = null;    private BufferedOutputStream out = null;    private Thread reciever;    private int port = 7000;    public Socket sock = null;    private ServerSocket ssock = null;    private String type;    private String file;    private String host;    private boolean resume = false;    public boolean finished = false;    private boolean isThere = false;    private long start;    private FtpConnection con;    private int skiplen = 0;    private boolean justStream = false;    private boolean ok = true;    private String localfile = null;    public DataConnection(FtpConnection con, int port, String host,                          String file, String type)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        reciever = new Thread(this);        reciever.start();    }    public DataConnection(FtpConnection con, int port, String host,                          String file, String type, boolean resume)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        this.resume = resume;        //resume = false;        reciever = new Thread(this);        reciever.start();    }    public DataConnection(FtpConnection con, int port, String host,                          String file, String type, boolean resume,                          boolean justStream)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        this.resume = resume;        this.justStream = justStream;        //resume = false;        reciever = new Thread(this);        reciever.start();    }    public DataConnection(FtpConnection con, int port, String host,                          String file, String type, boolean resume,                          String localfile)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        this.resume = resume;        this.localfile = localfile;        //resume = false;        reciever = new Thread(this);        reciever.start();    }    public DataConnection(FtpConnection con, int port, String host,                          String file, String type, boolean resume, int skiplen)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        this.resume = resume;        this.skiplen = skiplen;        //resume = false;        reciever = new Thread(this);        reciever.start();    }    public DataConnection(FtpConnection con, int port, String host,                          String file, String type, boolean resume,                          int skiplen, InputStream i)    {        this.con = con;        this.file = file;        this.host = host;        this.port = port;        this.type = type;        this.resume = resume;        this.skiplen = skiplen;        if(i != null)        {            this.in = new BufferedInputStream(i);        }        //resume = false;        reciever = new Thread(this);        reciever.start();    }    public void run()    {        try        {            if(Settings.getFtpPasvMode())            {                try                {                    sock = new Socket(host, port);                    sock.setSoTimeout(Settings.getSocketTimeout());                }                catch(Exception ex)                {                    ok = false;                    debug("Can't open Socket on port " + port);                }            }            else            {                //Log.debug("trying new server socket: "+port);                try                {                    ssock = new ServerSocket(port);                }                catch(Exception ex)                {                    ok = false;                    Log.debug("Can't open ServerSocket on port " + port);                }            }        }        catch(Exception ex)        {            debug(ex.toString());        }        isThere = true;        boolean ok = true;        try        {            if(!Settings.getFtpPasvMode())            {                int retry = 0;                while((retry++ < 5) && (sock == null))                {                    try                    {                        ssock.setSoTimeout(Settings.connectionTimeout);                        sock = ssock.accept();                    }                    catch(IOException e)                    {                        sock = null;                        debug("Got IOException while trying to open a socket!");                        if(retry == 5)                        {                            debug("Connection failed, tried 5 times - maybe try a higher timeout in Settings.java...");                        }                        throw e;                    }                    finally                    {                        ssock.close();                    }                    debug("Attempt timed out, retrying...");                }            }            if(ok)            {                isThere = true;                byte[] buf = new byte[Settings.bufferSize];                start = System.currentTimeMillis();                int buflen = 0;                if(type.equals(GET) || type.equals(GETDIR))                {                    RandomAccessFile fOut = null;                    BufferedOutputStream bOut = null;                    if(!justStream)                    {                        try                        {                            if(resume)                            {                                File f = new File(file);                                fOut = new RandomAccessFile(file, "rw");                                fOut.skipBytes((int) f.length());                                buflen = (int) f.length();                            }                            else                            {                                if(localfile == null)                                {                                    localfile = file;                                }                                File f = new File(localfile);                                if(f.exists())                                {                                    f.delete();                                }                                bOut = new BufferedOutputStream(new FileOutputStream(localfile),                                                                Settings.bufferSize);                            }                        }                        catch(Exception ex)                        {                            debug("Can't create outputfile: " + file);                            ok = false;                            ex.printStackTrace();                        }                    }                    if(ok)                    {                        try                        {                            in = new BufferedInputStream(sock.getInputStream(),                                                         Settings.bufferSize);                            if(justStream)                            {                                return;                            }                        }                        catch(Exception ex)                        {                            ok = false;                            debug("Can't get InputStream");                        }                        if(ok)                        {                            try                            {                                int len = buflen;                                if(fOut != null)                                {                                    while(true)                                    {                                        // resuming                                        int read;                                        try                                        {                                            read = in.read(buf);                                        }                                        catch(IOException es)                                        {                                            Log.out("got a IOException");                                            ok = false;                                            fOut.close();

⌨️ 快捷键说明

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