Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1

Java ftp实现文件的上传和下载

ftp,sftp

运行后发现很长时间没有反应,很久以后抛出如下异常:sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1

 

但是在cmdftp是可以通过的,用SecureCRT也是可以连接的,查看SecureCRT配置,看到端口22.

 

用jdk自带包实现代码如下:

publicclass FtpTest {

 

    private FtpClient ftpclient;

 

    publicvoid connectServer(String ip, int port, String username,

            String password, String path) {

        try {

            if (null == ip) {

                thrownew Exception("The service's ip is null!");

            }

            ftpclient = new FtpClient();

            // 当端口号为0时,采用默认端口号

            if(port == 0){

                ftpclient.openServer(ip);

            }else{

                ftpclient.openServer(ip, port);

            }         

            //ftpclient = new FtpClient(ip, port);      

            System.out.println(ftpclient.getFtpProxyPort());

            ftpclient.login(username, password);

            System.out.println("12324");

            ftpclient.binary();

            if (null != path) {

                ftpclient.cd(path); // 跳转到服务器的指定目录

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }  

    publicvoid connectServer(String ip, String username,

            String password, String path){

        connectServer(ip,0,username,password,path);

    }

    publicvoid closeServer() {

        if (null != ftpclient) {

            try {

                ftpclient.closeServer();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    publicvoid ftpupload(String localfilanme, String remotefilename) {

        FileInputStream input = null;

        TelnetOutputStream out = null;

        try {

            if (null == localfilanme || null == remotefilename) {

                thrownew Exception("文件名为空!");

            }

            File localfile = new File(localfilanme);

            if (!localfile.exists() || !localfile.isFile()

                    || !localfile.canRead()) {

                thrownew Exception("文件不存在或者不是文件!");

            }

            input = new FileInputStream(localfile);

            out = ftpclient.put(remotefilename);

            int c = 0;

            byte[] bye = newbyte[1024];

            while ((c = input.read(bye)) != -1) {

                out.write(bye, 0, c);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != input) {

                    input.close();

                }

                if (null != out) {

                    out.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    publicvoid ftpdownload(String localfilename, String remotefilename) {

        FileOutputStream out = null;

        TelnetInputStream input = null;

        try {

            if (null == localfilename || null == remotefilename) {

                thrownew Exception("文件名为空!");

            }

            File localfile = new File(localfilename);

            out = new FileOutputStream(localfile);

            input = ftpclient.get(remotefilename);

            int c = 0;

            byte[] bye = newbyte[1024];

            while ((c = input.read(bye)) != -1) {

                out.write(bye, 0, c);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != input) {

                    input.close();

                }

                if (null != out) {

                    out.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

 

        }

    }

    publicstaticvoid main(String[] args) {

        FtpTest test = new FtpTest();

        String ip = "xxx";

        String username = "admin";

        String password = "admin";

        String path = "/usr/admin/test";

        test.connectServer(ip,22,username, password, path);

        System.out.println(test.ftpclient.getFtpProxyPort());

        test.ftpupload("C:\\Users\\admin\\Desktop\\123.txt", "123.txt");

        test.ftpdownload("D:\\124.txt", "123.txt");

        test.closeServer();

    }

}

 

网上找了很久,没找到什么资料。通过查询ftp资料发,有这样一句解释:FTP服务一般运行在20和21两个端口。端口20用于传输控制流,而端口21用于在客户端和服务器之间传输数据流。再看SecureCRT发现有一个sftp标签页配置,查询sftp资料,解释如下:sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。发现端口22是 sftp的默认端口,所以,将代码中的端口22改成21即可解决这个问题。

http://www.dexcoder.com/selfly/article/4028中,页讲解了用jsch包sftp的方式解决。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值