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

ftp,sftp

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

 

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

 

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

publicclass FtpTest {
 
privateftpclient;
 
publicvoidint port, String username,
            String password, String path) {
try {
ifnull == ip) {
thrownew"The service's ip is null!");
            }
ftpclientnew FtpClient();
// 当端口号为0时,采用默认端口号
if(port == 0){
ftpclient.openServer(ip);
else{
ftpclient.openServer(ip, port);
            }         
//ftpclient = new FtpClient(ip, port);      
out.println(ftpclient.getFtpProxyPort());
ftpclient.login(username, password);
out.println("12324");
ftpclient.binary();
ifnull != 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() {
ifnullftpclient) {
try {
ftpclient.closeServer();
 catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
publicvoid ftpupload(String localfilanme, String remotefilename) {
null;
null;
try {
ifnullnull == remotefilename) {
thrownew"文件名为空!");
            }
new File(localfilanme);
if (!localfile.exists() || !localfile.isFile()
                    || !localfile.canRead()) {
thrownew"文件不存在或者不是文件!");
            }
new FileInputStream(localfile);
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 {
ifnull != input) {
                    input.close();
                }
ifnull != out) {
                    out.close();
                }
 catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
publicvoid ftpdownload(String localfilename, String remotefilename) {
null;
null;
try {
ifnullnull == remotefilename) {
thrownew"文件名为空!");
            }
new File(localfilename);
new FileOutputStream(localfile);
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 {
ifnull != input) {
                    input.close();
                }
ifnull != out) {
                    out.close();
                }
 catch (IOException e) {
                e.printStackTrace();
            }
 
        }
    }
publicstaticvoid main(String[] args) {
new FtpTest();
"xxx";
"admin";
"admin";
"/usr/admin/test";
        test.connectServer(ip,22,username, password, path);
out.println(test.ftpclient.getFtpProxyPort());
"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的方式解决。