JAVA利用FTPClient以及SFTPClient实现FTP文件两台FTP服务器文件复制

最近工作要求两台FTP服务器之间的文件复制,查询资料后以及个人使用后,整理JAVA的FTPClient以及SFTPClient两个工具类相关内容,方便以后使用。

一开始本来使用FTPClient工具类进行两台服务器之间的文件交互,但是由于公司部分服务器拒绝FTP连接,使用的是SFTP连接,所以最后换成了SFTPClient工具类

例:复制 192.168.1.133 的 /root/tmp 目录下的所有文件到 192.168.1.134 的 /root/tmp 目录下
FTPClient实现:

String filePath = "/root/tmp";
String host1 = "192.168.1.133";
String host2 = "192.168.1.134";
String username = "root";
String password = "root";
//实例化FTPClient对象
FTPClient ftpClient1 = new FTPClient();
FTPClient ftpClient2 = new FTPClient();
try{
	//连接FTP服务器
	ftpClient1 .connect(host1);
	ftpClient2 .connect(host2);
	//登录FTP服务器
	CjpftpClient1 .login(username,password);
	ftpClient2 .login(username,password);
	//获取FTP服务器路径下的所有文件
	FTPClient[] ftpFiles = ftpClient.listFiles(filePath);
	for(FTPClient ftpFile : ftpFiles){
		//获取文件名称
		String fileName = new String(ftpFile.getName().getBytes("ISO-8859-1"),"UTF-8");
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ftpClient1.setBufferSize(1024);
		//更改FTP服务器的工作路径
		ftpClient1.changeWorkingDirectory(filePath);
		//读取文件到内存中
		ftpClient1.get(fileName,os);
		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		//如果输入流不为空,复制文件
		if(is != null){
			ftpClient2.storeFile(filePath + "/" + fileName,is);
		}
		is.close();
		os.close();
	}
	ftpClient1.logout();
	ftpClient2.logout();
}catch(Exception e){
	e.printStackTrace();
}

SFTP实现:
定义工具类ConnectToClientTools

public class ConnectToClientTools{
	private ChannelSftp sftp = new ChannelSftp();
    private Session sshSession;

    /**
     * 创建SFTP连接
     * @param host
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    public ChannelSftp connectBySftp(String host, String username, String password) throws Exception {
        try{
            JSch jsch = new JSch();
            sshSession = jsch.getSession(username,host);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking","no");
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            return sftp;
        } catch (Exception e) {
            sftp.disconnect();
            sftp = null;
            throw e;
        }
    }

    /**
     * 关闭SFTP连接
     */
    public void logOut(){
        if (sftp != null){
            if (sftp.isConnected()){
                sftp.disconnect();
            }
        }
        if (sshSession != null){
            if (sshSession.isConnected()){
                sshSession.disconnect();
            }
        }
    }
}

功能实现:

		String filePath = "/root/tmp";
		String host1 = "192.168.1.133";
		String host2 = "192.168.1.134";
		String username = "root";
		String password = "root";
		ConnectToClientTools sftpClient1 = new ConnectToClientTools();
		ConnectToClientTools sftpClient2 = new ConnectToClientTools();

        try {
            //连接服务器
            ChannelSftp sftp1 = sftpClient1.connectBySftp(host1,username,password);
            ChannelSftp sftp2 = sftpClient2.connectBySftp(host2,username,password);

            Vector<?> ftpFiles = sftp1.ls(filePath);
            for (Object ftpFile : ftpFiles){
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) ftpFile;
                String fileName = new String(entry.getFilename().getBytes("ISO-8859-1"),"UTF-8");
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                sftp1.cd(filePath);
                sftp1.get(fileName,os);
                ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
                if (is != null){
                    try {
                        sftp2.cd(filePath);
                    } catch (SftpException e) {
                        sftp2.mkdir(filePath);
                        sftp2.cd(filePath);
                    }
                    sftp2.put(is,fileName);
                }
            }
            sftpClient1.logOut();
            sftpClient2.logOut(); 
        } catch (Exception e) {
            e.printStackTrace();
        }

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法,如:
put(): 文件上传
get(): 文件下载
cd(): 进入指定目录
ls(): 得到指定目录下的文件列表
rename(): 重命名指定文件或目录
rm(): 删除指定文件
mkdir(): 创建目录
rmdir(): 删除目录

你可能感兴趣的:(JAVA)