java调用SCP,文件远程服务器复制到本地

一. 依赖ganymed-ssh2-build210.jar
二.代码
下面是复制整个目录中文件的代码,(也可以复制单个或指定后缀名的文件,需要改动)

package zexcel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3DirectoryEntry;
import ch.ethz.ssh2.SFTPv3FileAttributes;
import java.io.File;

public class SCPUtilsTwo{
	
	private static int port = 22;//目标服务器端口
	private static String basePath="/Tax";//目标服务器目录
	private static String copyHost="127.0.0.1";//目标服务器地址
	private static String copyUsername="root"; //目标服务器用户名
	private static String copyPassword="123456";//目标服务器密码
	private static String localFile="/Tax";//本地服务器指定目录//liux环境下会创建到根目录下
	
	public static void SCPInit(){
		String batchNo="2018000054";//文件夹名称
		Long d1 =Calendar.getInstance().getTime().getTime();
		System.out.println("localFile============"+localFile);
		System.out.println();
		File file=new File(localFile+"/"+batchNo);
		if(!file.exists()){
			file.mkdirs();
		}
		System.out.println("创建成功!");
		Connection conn=null;
		List remoteFile=new ArrayList();
		System.out.println("copyHost==="+copyHost);
		System.out.println("copyUsername==="+copyUsername);
		System.out.println("copyPassword==="+copyPassword);
		System.out.println("port==="+port);
		try{
			conn = new Connection(copyHost,port);
			System.out.println("开始连接");
			conn.connect();
			System.out.println("开始登录");
			boolean isAuthenticated = conn.authenticateWithPassword(copyUsername, copyPassword);
			if (isAuthenticated == false)
				throw new IOException("Authentication failed.");
			
			SFTPv3Client sftpv3Client = new SFTPv3Client(conn);
			
			//查看目标服务器此文件夹是否存在
			Boolean b = SCPUtilsTwo.isDir(sftpv3Client, basePath+"/"+batchNo);
			if(!b){
				System.out.println("文件夹不存在");
			}else{
				//获取目标文件夹中所有文件
				Vector v =sftpv3Client.ls(basePath+"/"+batchNo);
				Iterator it = v.iterator();
				while (it.hasNext()) {
					 SFTPv3DirectoryEntry entry = ( SFTPv3DirectoryEntry) it.next();
					String filename = entry.filename;
				    remoteFile.add(basePath+"/"+batchNo+"/"+filename);
				}
				SCPClient client= conn.createSCPClient();
				String [] remoteFiless=new String[remoteFile.size()];
				String[] remoteFiles =remoteFile.toArray(remoteFiless);
				//开始上传
				client.get(remoteFiles, localFile+"/"+batchNo);
				System.out.println("上传完成");
				Long d2 =Calendar.getInstance().getTime().getTime();
				Long m=(d2-d1)/1000;
				System.out.println("总耗时==="+m+"秒");
			}
		}catch (IOException e){
			System.out.println("文件上传异常"+e);
		}finally{
			conn.close();
		}
		
	}
	public static Boolean isDir(SFTPv3Client sftpv3Client,String path) {
		if(sftpv3Client != null && path!=null && ("").equals(path)) {
			SFTPv3FileAttributes sFTPv3FileAttributes;
			try {
				sFTPv3FileAttributes = sftpv3Client.lstat(path);
			} catch (IOException e) {
				System.out.println("文件加不存在:"+e.getLocalizedMessage());
				return false;
			}
			return sFTPv3FileAttributes.isDirectory();
		}
		return false;
	}
}

你可能感兴趣的:(java随笔)