欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java编写ftp下载工具

程序员文章站 2024-03-03 21:09:58
需要用到 java 写一个 ftp 的工具,因为只有一点点 java 基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。 不过因为是在 linux...

需要用到 java 写一个 ftp 的工具,因为只有一点点 java 基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。

不过因为是在 linux 下使用 javac 编译,不是在 win 下使用 ide 来做这些事情,所以在运行和编译上又费了一些时间,不过正是因为这样对 java 的一些编译、运行的知识又了解了一些。

对于 ftp 下载工具,代码如下:

复制代码 代码如下:

import java.io.file;  
import java.io.fileinputstream;  
import java.io.filenotfoundexception;  
import java.io.fileoutputstream;  
import java.io.ioexception;  
import java.io.inputstream;  
import java.io.outputstream;  
import java.net.socketexception;  
import org.apache.commons.net.ftp.ftpclient;  
import org.apache.commons.net.ftp.ftpreply;  
public class ftpclient {
    private string         host;  
    private int            port;  
    private string         username;  
    private string         password;  
    private boolean        binarytransfer = true;  
    private boolean        passivemode    = true;  
    private string         encoding       = "utf-8";  
    private int            clienttimeout  = 3000;  
    private boolean flag=true;
    private ftpclient ftpclient = null;
    public string gethost() {  
        return host;  
    }  
    public void sethost(string host) {  
        this.host = host;  
    }  
    public int getport() {  
        return port;  
    }  
    public void setport(int port) {  
        this.port = port;  
    }  
    public string getusername() {  
        return username;  
    }  
    public void setusername(string username) {  
        this.username = username;  
    }  
    public string getpassword() {  
        return password;  
    }  
    public void setpassword(string password) {  
        this.password = password;  
    }  
    public boolean isbinarytransfer() {  
        return binarytransfer;  
    }  
    public void setbinarytransfer(boolean binarytransfer) {  
        this.binarytransfer = binarytransfer;  
    }  
    public boolean ispassivemode() {  
        return passivemode;  
    }  
    public void setpassivemode(boolean passivemode) {  
        this.passivemode = passivemode;  
    }  
    public string getencoding() {  
        return encoding;  
    }  
    public void setencoding(string encoding) {  
        this.encoding = encoding;  
    }  
    public int getclienttimeout() {  
        return clienttimeout;  
    }  
    public void setclienttimeout(int clienttimeout) {  
        this.clienttimeout = clienttimeout;  
    }  
    public ftpclient(string host) {
        this.username = "anonymous";
        this.encoding = "utf-8";
        this.binarytransfer = true;
        this.binarytransfer = true;
        this.port = 21;
        this.host = host;
        try {
            this.ftpclient = getftpclient();
        } catch (exception e) {
            system.out.println("create ftpclient error!");
        }
    }
    private ftpclient getftpclient() throws ioexception {  
        ftpclient ftpclient = new ftpclient();
        ftpclient.setcontrolencoding(encoding);
        connect(ftpclient);
        if (passivemode) {  
            ftpclient.enterlocalpassivemode();  
        }  
        setfiletype(ftpclient);
        try {  
            ftpclient.setsotimeout(clienttimeout);  
        } catch (socketexception e) {  
            throw new ioexception("set timeout error.", e);  
        }  
        return ftpclient;  
    }  
    private void setfiletype(ftpclient ftpclient) throws ioexception {  
        try {  
            if (binarytransfer) {  
                ftpclient.setfiletype(ftpclient.binary_file_type);  
            } else {  
                ftpclient.setfiletype(ftpclient.ascii_file_type);  
            }  
        } catch (ioexception e) {  
            throw new ioexception("could not to set file type.", e);  
        }  
    }  
    public boolean connect(ftpclient ftpclient) throws ioexception {  
        try {  
            ftpclient.connect(host, port);  
            int reply = ftpclient.getreplycode();  
            if (ftpreply.ispositivecompletion(reply)) {  
                if (ftpclient.login(username, password)) {  
                    setfiletype(ftpclient);  
                    return true;  
                }  
            } else {  
                this.ftpclient.disconnect();  
                throw new ioexception("ftp server refused connection.");  
            }  
        } catch (ioexception e) {  
            if (this.ftpclient.isconnected()) {  
                try {  
                    this.ftpclient.disconnect();
                } catch (ioexception e1) {  
                    throw new ioexception("could not disconnect from server.", e);  
                }  
            }  
            throw new ioexception("could not connect to server.", e);  
        }  
        return false;  
    }  
    private void disconnect() throws ioexception {  
        try {  
            this.ftpclient.logout();  
        } catch (ioexception e) {  
            system.out.println("logout may timeout!");
        } finally {
            if (this.ftpclient.isconnected()) {  
                this.ftpclient.disconnect();  
            }  
        } 
    }  
    public inputstream getstream(string serverfile) throws ioexception {
        inputstream instream = null;
        try {
            instream = this.ftpclient.retrievefilestream(serverfile);
            system.out.println("instream get over!");
            return instream;
        } catch (ioexception e) {
            system.out.println("get stream exception");
            return null;
        }
    }
    public boolean writestream(inputstream input, string localfile) throws ioexception {
        fileoutputstream fout = new fileoutputstream(localfile);
        int ch = 0;
        if(input == null){
            system.out.println("input is null");
            return false;
        }
        try {
            ch = input.read();
            while(ch != -1){
                fout.write(ch);
                ch = input.read();
            }
            system.out.println("write over!");
            return flag;
        } catch (ioexception e) {
            throw new ioexception("couldn't get file from server.", e);
        }
    }
    public boolean isexist(string remotefilepath)throws ioexception{
        try{
            file file=new file(remotefilepath);
            string remotepath=remotefilepath.substring(0,(remotefilepath.indexof(file.getname())-1));
            string[] listnames = this.ftpclient.listnames(remotepath);  
            system.out.println(remotefilepath);
            for(int i=0;i<listnames.length;i++){
                system.out.println(listnames[i]);
                if(remotefilepath.equals(listnames[i])){
                    flag=true;
                    system.out.println("file:"+file.getname()+" existed");
                    break;
                }else {
                    flag=false;
                }
            }
        } catch (ioexception e) {  
            throw new ioexception("file exception", e);  
        }
        return flag;
    }
    //main for testing
    public static void main(string[] args) throws ioexception {  
        string hostname = "cp01-testing-ps7130.cp01.baidu.com";
        string serverfile="/home/work/check_disk.sh";
        string localfile="/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
        ftpclient ftp = new ftpclient(hostname);  
        system.out.println(ftp.isexist(serverfile));
        ftp.writestream(ftp.getstream(serverfile), localfile);
        ();
    }  
}

这个工具是为了配合另外一个 hadoop 工具做 集群上传用的,所以里面的把 input 和 output 流分开了,也是为了方便另外一个工具使用。

补充一点,如何在 linux 配置运行:

如果这样的代码需要在 linux 下环境运行,首先要配置好响应的包,例如

复制代码 代码如下:

import org.apache.commons.net.ftp.ftpclient;

这个包在 apache 的网站上直接下载就行,解压后找到对应的 jar 包,在编译的时候进行引用:

复制代码 代码如下:

export ftppath="${路径}/xxx.jar"
javac -classpath $classpath:$ftppath ftpclient.java

同样,在运行的时候也要指定 classpath:

复制代码 代码如下:

java -classpath $classpath:$ftppath ftpclient

建议不要把$ftppath 包含在 classpath 中,用什么包就引用什么环境变量就行了,没必要一股脑都添加进去,就像我们没必要 import 所有的包一样。

以上所述就是本文的全部内容了,希望能够对大家学习java有所帮助。

请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!