Jftp上传文件实例

使用开源Jftp开发适合ftp和sftp两种模式的文件传输,实现类如下:


import  net.sf.jftp.config.Settings;
import  net.sf.jftp.net.ConnectionHandler;
import  net.sf.jftp.net.ConnectionListener;
import  net.sf.jftp.net.FtpConnection;
import  net.sf.jftp.net.BasicConnection;
import  net.sf.jftp.net.SftpConnection;

import  java.util.HashMap;

import  com.sshtools.j2ssh.configuration.SshConnectionProperties;

public   class  JftpUtil  implements  ConnectionListener  {

    
private boolean isThere = false;

    
private ConnectionHandler handler = new ConnectionHandler();

    
private HashMap connectPool = new HashMap();
    
    
private Settings setting = new Settings();

    
public BasicConnection initConnection(String serverType, String host, int port, String user, String passwd) {
        setting.setProperty(
"jftp.disableLog"true);//设置Log功能关闭,关闭控制台msg
        
if(serverType.equalsIgnoreCase("ftp")){//建立ftp链接
            FtpConnection con 
= new FtpConnection(host, port, "/");
            con.addConnectionListener(
this);
            con.setConnectionHandler(handler);
            con.login(user, passwd);
            
            
while (!isThere) {
                
try {
                    Thread.sleep(
10);
                }
 catch (Exception ex) {
                    ex.printStackTrace();
                }

            }

            
if (isThere) {
                
return con;
            }
 else {
                
return null;
            }

        }
else{//建立sftp连接
            SftpConnection sftp_con 
= null;
            SshConnectionProperties pro 
= new SshConnectionProperties();
            pro.setHost(host);
            pro.setPort(port);
            sftp_con 
= new SftpConnection(pro);
            sftp_con.addConnectionListener(
this);
            
if(sftp_con.login(user, passwd)){
                
return sftp_con;
            }
else{
                
return null;
            }


        }

        
    }


    @SuppressWarnings(
"unchecked")
    
public BasicConnection getConnection(String serverType, String host, int port, String user, String passwd){
        
if (this.connectPool.isEmpty()) {
            BasicConnection tmp 
= this.initConnection(serverType, host, port, user, passwd);
                
if(tmp == null || !tmp.isConnected()){
                    
return null;
                }
else{
                    
this.connectPool.put(host, tmp);
                    
return tmp;
                }

        }
 else {
            BasicConnection connection 
= (BasicConnection) this.connectPool.get(host);
            
if (connection != null{
                
return connection;
            }
 else {
                BasicConnection tmp 
= this.initConnection(serverType, host, port, user, passwd);
                
if(tmp == null || !tmp.isConnected()){
                    
return null;
                }
else{
                    
return tmp;
                }

            }

        }

    }


    
public boolean ftpUpload(BasicConnection con, String dir, String file) {
        
        
if(con == null){
            
return false;
        }
else{
            
//make dirs
            String path = "";
            String[] paths 
= dir.split("/");
            
for (int i = 0; i < paths.length; i++{
                path 
+= "/" + paths[i];
                
if (!con.chdir(path)) {
                    con.mkdir(path);
                }

            }

            con.chdir(dir);
            con.upload(file);
            
return true;
        }

    }

    
    
public boolean sftpUpload(BasicConnection con, String destination, String source, String file) {
        
        
if(con == null){
            
return false;
        }
else{
            
//make dirs
            String path = "";
            String[] paths 
= destination.split("/");
            
for (int i = 0; i < paths.length; i++{
                path 
+= "/" + paths[i];
                
if (!con.chdir(path)) {
                    con.mkdir(path);
                }

            }

            con.chdir(destination);
            con.setLocalPath(source);
            con.upload(file);
            
return true;
        }

    }


    
public void release() throws Exception {
        
try {
            
if(connectPool!=null && !connectPool.isEmpty()){
                Object[]obj 
= connectPool.values().toArray();
                
for(int i=0;i<obj.length;i++){
                    BasicConnection connect 
= (BasicConnection)obj[i];                    
                    
if(connect != null && connect.isConnected()){
                        connect.disconnect();
                    }

                }

                connectPool.clear();
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    
public static void main(String argv[]) {

        JftpUtil g 
= new JftpUtil();
        BasicConnection conn;
        conn 
= g.getConnection("sftp""192.168.1.121"22"lin.ye""yehell");
        
        
// g.ftpUpload(conn, "/home/lin.ye", "e:/tmp/test1.txt");
        g.sftpUpload(conn, "/home/lin.ye""e:/tmp""JftpUtil.java");

        
try {
            g.release();
        }
 catch (Exception e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    
public void updateRemoteDirectory(BasicConnection con) {
        System.out.println(
"new path is: " + con.getPWD());
    }


    
public void connectionInitialized(BasicConnection con) {
        isThere 
= true;
    }


    
public void updateProgress(String file, String type, long bytes) {
    }


    
public void connectionFailed(BasicConnection con, String why) {
        System.out.println(
"connection failed!");
    }


    
public void actionFinished(BasicConnection con) {
    }


    
public void debug(String msg) {
        System.out.println(msg);
    }


    
public void debugRaw(String msg) {
        System.out.print(msg);
    }


    
public void debug(String msg, Throwable throwable) {
    }


    
public void warn(String msg) {
    }


    
public void warn(String msg, Throwable throwable) {
    }


    
public void error(String msg) {
    }


    
public void error(String msg, Throwable throwable) {
    }


    
public void info(String msg) {
    }


    
public void info(String msg, Throwable throwable) {
    }


    
public void fatal(String msg) {
    }


    
public void fatal(String msg, Throwable throwable) {
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值