java scp传输文件

 <dependency>
     <groupId>ch.ethz.ganymed</groupId>  
    <artifactId>ganymed-ssh2</artifactId>  
    <version>build210</version>  
  </dependency>



import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPException;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;


public class SCPUtil {

	 
	public static final String LOCALPATH = "";
	
	public static final String REMOTEFILEPATH = "";

	public static final String REMOTEIP = "";

	public static final String USERNAME = "";

	public static final String PASSWORD = "";

	/**
	 * 远程文件传输,如果local参数是文件,则本地传输到远程;如果是目录,则远程传输到本地
	 * 
	 * @param remoteIp
	 *            远程主机IP或hostname
	 * @param user
	 *            远程主机用户名
	 * @param password
	 *            远程主机对应密码
	 * @param local
	 *            本地主机文件名(本地->远程);本地主机目录名(远程->本地)
	 * @param remote
	 *            远程主机目录名(本地->远程);远程主机文件名(远程->本地)
	 * @return 返回true:成功。false:失败
	 */
	public static boolean scpFile(String remoteIp, String user,
			String password, String local, String remote) {
		Connection con = new Connection(remoteIp);
		try {
			con.connect();
			boolean isAuthed = con.authenticateWithPassword(user, password);

			if (!isAuthed) {
				//logger.error("远程主机" + remoteIp + "用户名或密码验证失败!");
				return false;
			}

			SCPClient scpClient = con.createSCPClient();
			File localFile = new File(local);
			if (localFile.isFile()) {
				if (!localFile.exists()) {
					//logger.error("本地文件" + local + "不存在,无法传输!");
					return false;
				} else {
					try {
						SFTPv3Client sftpClient = new SFTPv3Client(con);
						// 远程新建目录
						sftpClient.mkdir(remote, 0777);
					} catch (SFTPException e1) {
					//	logger.info("目录" + remote + "已存在,无需再创建。");
					}

					try {
						scpClient.put(local, remote, "0777");
					} catch (IOException e2) {
						//logger.error("路径" + remote + "不是一个文件夹。");
						return true;
					}

					// 上传到远程主机上的文件
					String filename = local
							.substring(local.lastIndexOf('/') + 1);
					Session session = con.openSession();
					session.execCommand("ls -l " + remote + "/" + filename);
					InputStream is = new StreamGobbler(session.getStdout());
					BufferedReader br = new BufferedReader(
							new InputStreamReader(is));
					String line = br.readLine();
					br.close();
					is.close();
					session.close();
					if (!StringUtils.isEmpty(line)) {
						return true;
					}
				}
			} else {
				if (!localFile.exists()) {
					localFile.mkdirs();
				}

				scpClient.get(remote, local);

				String filename = remote.substring(remote.lastIndexOf('/') + 1);
				if (new File(local + "/" + filename).exists()) {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		con.close();
		return false;
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值