java scp传输文件

java scp传输文件<dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>build210</version></dependency&a

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

 <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;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171994.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 诺基亚手机软件测试工具_诺基亚刷安卓

    诺基亚手机软件测试工具_诺基亚刷安卓手机软件测试  目录1手机知识…31.1手机的主要功能…31.1.1通话功能…31.1.2消息功能…31.1.3电话本…31.1.4增值服务…31.1.5其他功能…31.1.6为特定语言定做的功能…41.1.7附件…41.2手机的软件结构…41.3手机的硬件结构

    2025年9月17日
    6
  • C#多线程锁「建议收藏」

    C#多线程锁「建议收藏」c#多线程操作队列

    2022年6月21日
    32
  • API接口签名验证

    API接口签名验证

    2021年10月13日
    48
  • java+widthstep,i*step+j*channels+k 以及widthStep大小计算及原理

    java+widthstep,i*step+j*channels+k 以及widthStep大小计算及原理一直以为IplImage结构体中的widthStep元素大小等于width*nChannels,大错特错!查看OpenCV2.1的源码,在src/cxcore/cxarray.cpp文件中,找到cvInitImageHeader函数,函数中对widthStep大小赋值如下:image->widthStep=(((image->width*image->nChannels…

    2022年6月9日
    39
  • springboot框架简介_springboot工作原理

    springboot框架简介_springboot工作原理springboot介绍1、springboot概述springboot对spring的缺点进行改善和优化,基于约定优化配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发效率,也可以缩短项目周期。2、springboot特点为基于spring开发提供更快的入门体验。开箱即用,不需要代码生成,也不需要xml配置,同时也可以修改默认值来满足特定的需求。提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部

    2022年8月20日
    8
  • Windows中Pycharm中配置Qt[通俗易懂]

    Windows中Pycharm中配置Qt[通俗易懂]Qt是什么Qt是Python开发窗体的工具之一,它与python有着良好的兼容性,且可通过可视化拖曳的方式进行窗体的创建,提高开发效率。Qt工具支持Windows,Linux,MacOSX三种操作系统。可用pipinstallpyqt5进行在线安装配置Pycharm由于QtDesigner在设计窗体时,后缀名为ui,需要转为py文件后才可以被…

    2022年8月29日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号