J2SSH Maverick 一段连接sftp服务器,上传,下载的代码:

Java中通过sftp协议实现上传下载 在java开发中,遇到需要将linux系统中指定目录下的文件下载到windows本地的需求,下面聊聊通过sftp协议实现上传和下载。 阅读详情

J2SSH Maverick
Version 1.2.6

一段连接sftp服务器,上传,下载的代码:
import com.maverick.ssh.*;
import com.maverick.ssh1.Ssh1Client;
import com.maverick.ssh2.*;
import java.io.*;
import com.sshtools.net.*;
import com.sshtools.sftp.*;
import com.maverick.sftp.*;
import com.sshtools.publickey.ConsoleKnownHostsKeyVerification;
/**
 * This example demonstrates the connection process connecting to an SSH2 server and
 * usage of the SFTP client.
 *
 * @author Lee David Painter
 * @version $Id: SftpConnect.java,v 1.8 2006/01/30 17:46:04 lee Exp $
 *
 */
public class SftpConnect {
     public static void main(String[] args) {
          final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          String str = "user.home";

          try {
               System.out.print("Hostname: ");
              // String hostname = reader.readLine();
               String hostname = "192.168.1.237";
               int idx = hostname.indexOf(':');
               int port = 22;
               if(idx > -1) {
                    port = Integer.parseInt(hostname.substring(idx+1));
                    hostname = hostname.substring(0, idx);
               }
               System.out.print("Username [Enter for " + System.getProperty("user.name") + "]: ");
               //String username = reader.readLine();
               String username = "linuxborder";
               if(username==null || username.trim().equals(""))
                    username = System.getProperty("user.name");
               System.out.println("Connecting to " + hostname);
               /**
                * Create an SshConnector instance
                */
               SshConnector con = SshConnector.getInstance();
               // Lets do some host key verification
               con.getContext(SshConnector.SSH2).setHostKeyVerification(new ConsoleKnownHostsKeyVerification());
               // Set the preferred cipher to AES for best transfer performance
               Ssh2Context ssh2Context = (Ssh2Context)con.getContext(SshConnector.SSH2);
               ssh2Context.setPreferredPublicKey(Ssh2Context.PUBLIC_KEY_SSHDSS);
               ssh2Context.supportedCiphers().add("aes128-cbc", com.sshtools.cipher.AES128Cbc.class);
               ssh2Context.setPreferredCipherCS("aes128-cbc");
               ssh2Context.setPreferredCipherSC("aes128-cbc");
               /**
                * Connect to the host
                */
               SocketTransport t = new SocketTransport(hostname, port);
               t.setTcpNoDelay(true);
               SshClient ssh = con.connect(t, username);
               /**
                * Determine the version
                */
               if(ssh instanceof Ssh1Client) {
                    System.out.println(hostname + " is an SSH1 server!! SFTP is not supported");
                    ssh.disconnect();
                    System.exit(0);
               }
               else
                    System.out.println(hostname + " is an SSH2 server");
               Ssh2Client ssh2 = (Ssh2Client)ssh;
               /**
                * Authenticate the user using password authentication
                */
               com.maverick.ssh.PasswordAuthentication pwd = new com.maverick.ssh.PasswordAuthentication();
               do {
                    System.out.print("Password: ");
                    pwd.setPassword("linuxborder");
                   // pwd.setPassword(reader.readLine());
               }
               while(ssh2.authenticate(pwd)!=SshAuthentication.COMPLETE
                         && ssh.isConnected());
               /**
                * Start a session and do basic IO
                */
               if(ssh.isAuthenticated()) {
                   SftpClient sftp = new SftpClient(ssh2);
                   System.out.println("******************************************");
                   System.out.println(" sftp.pwd() " +sftp.pwd());
                   sftp.mkdir("netsky");
                   System.out.println(" sftp.pwd() " +sftp.getAbsolutePath("netsky"));
                   sftp.cd("netsky");
                   /**
                    * Perform some text mode operations
                    */
                   sftp.setTransferMode(SftpClient.MODE_TEXT);
                   File textFile = new File(System.getProperty("user.home"), "shining.txt");
                   FileOutputStream tout = new FileOutputStream(textFile);
                   // Create a file with /r/n as EOL
                   for(int i=0;i<5;i++) {
                       tout.write("All work and no play makes Jack a dull boy/r/n".getBytes());
                   }
                   tout.close();
                   // Tell the client which EOL the remote client is using - note
                   // that this will be ignored with version 4 of the protocol
                   sftp.setRemoteEOL(SftpClient.EOL_LF);
                   // Now put the file, the remote file should end up with all /r/n changed to /n
                   sftp.put(textFile.getAbsolutePath());
                   /**
                    * Now perform some binary operations
                    */
                   sftp.setTransferMode(SftpClient.MODE_BINARY);
                   /**
                     * List the contents of the directory
                     */
                    SftpFile[] ls = sftp.ls();
                    for(int i=0;i<ls.length;i++) {
                      System.out.println(ls[i].toString());
                      System.out.println(SftpClient.formatLongname(ls[i]));
                    }
                      /**
                       * Generate a temporary file for uploading/downloading
                       */
                  
                      File f = new File(System.getProperty(str), "sftp-file");
                      java.util.Random rnd = new java.util.Random();
                      FileOutputStream out = new FileOutputStream(f);
                      byte[] buf = new byte[1024];
                      for(int i=0;i<30;i++) {
                        rnd.nextBytes(buf);
                        out.write(buf);
                      }
                      out.close();
                      /**
                     * Create a directory
                     */
                    sftp.mkdirs("sftp/test-files");
                    /**
                     * Change directory
                     */
                    sftp.cd("sftp/test-files");
                    /**
                     * Put a file into our new directory
                     */
                    long length = f.length();
                    System.out.println("Putting file");
                    long t1 = System.currentTimeMillis();
                    sftp.put(f.getAbsolutePath());
                    long t2 = System.currentTimeMillis();
                    System.out.println("Completed.");
                    long e = t2-t1;
                    System.out.println("Took " + String.valueOf(e) + " milliseconds");
                    float secs, kbs;
                    if(e >= 1000) {
                      kbs = ((float)length / 1024) / ((float)e / 1000);
                      System.out.println("Upload Transfered at " + String.valueOf(kbs) +
                                         " kbs");
                    }
                    /**
                     * Get the attributes of the uploaded file
                     */
                    System.out.println("Getting attributes of the remote file");
                    SftpFileAttributes attrs = sftp.stat(f.getName());
                    System.out.println(SftpClient.formatLongname(attrs, f.getName()));
                    /**
                     * Download the file inot a new location
                     */
                    File f2 = new File(System.getProperty(str), "downloaded");
                    f2.mkdir();
                    sftp.lcd(f2.getAbsolutePath());
                    System.out.println("Getting file");
                    t1 = System.currentTimeMillis();
                    sftp.get(f.getName());
                    t2 = System.currentTimeMillis();
                    System.out.println("Completed.");
                    e = t2-t1;
                    System.out.println("Took " + String.valueOf(e) + " milliseconds");
                    if(e >= 1000) {
                      kbs = ((float)length / 1024) / ((float)e / 1000);
                      System.out.println("Download Transfered at " + String.valueOf(kbs) +
                                         " kbs");
                    }

                /**
                 * Set the permissions on the file and check they were changed
                 * they should be -rw-r--r--
                 */
                sftp.chmod(0644, f.getName());
                attrs = sftp.stat(f.getName());
                System.out.println(SftpClient.formatLongname(attrs, f.getName()));
                System.out.println("******************************************");
                System.out.println(" sftp.pwd() " + sftp.pwd());
                sftp.quit();
                ssh.disconnect();
              }
               } catch(Throwable th) {
               th.printStackTrace();
          }
     }
}
实现SSH2安全远程操作的Java库详解 本文还有配套的精品资源,点击获取 简介:SSH2jar包是一种Java库,用于安全地实现SSH2协议,支持安全远程登录、文件传输及网络操作。它包含丰富的类和方法,便于开发人员在Java中安全执行命令、管理远程系统和数据交换。SSH2jar包提供了多种身份验证方式、连接会话管理、SFTP、端口转发、通道管理以及加密算法支持,还具备错误处理和调试功能。使用此库的开发步骤包括添加... 阅读详情

相关推荐

java源码api-j2ssh-maverick:我们的旧版API的开源分支为社区提供了强大的,关键任务SSH组件

java原始api J2SSH小牛 J2SSH Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将发布,此旧版API现在已在LGPL v3下发布,并将继续由核心作者维护,以对开源社区做出贡献。 执照 新增功能该项目现已获得LGPL v3许可,可用于商业和开源应用程序。 Maven仓库 现在可以在Maven Central上发布版本。 只需在pom中包括以下依赖项即可。 <dependency> <groupId>com.sshtools</groupId> <artifactId>j2ssh-maverick</artifactId> <version>1.5.5</version> </dependency> 二进制构建 可以在SourceForge.net上的以下URL上找到二进制版本。 文献资料 您可以在以下位置找到javadocs 示例包含在二进制下载中或源树的src / main / examples文

maverick组件实现JAVA SSH协议初探

最近研究用SSH协议访问远程Linux机器,采用最普通的用户帐号和口令连接方式,端口为默认的22,以及需要执行的shell命令。我google了下用java 实现的SSH实现方式,找到了一款商业软件J2SSH Maverick(看网站介绍,该软件就是原来sourceforge上的J2SSH的升级版,更强大,更稳定,效率更高),网站只允许试用45天,过后需要购买Licenses,网址是http://

pengchang_1981的专栏 5078

SSHTOOLS J2SSH Maverick:J2SSH的原始作者的第二代SSH API-开源

J2SSH Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将发布,该旧版API现在已在GPLv3下发布,并将继续由核心作者维护,作为对开源社区的贡献。

远程文件解析-j2ssh

j2ssh 是集成ssh、sftp的java的工具包,利用j2ssh基本上可以完成你在服务器操作的所有命令。今天只就sftp讲解。 maven 依赖包引入

wang972779876的博客 995

J2SSH(Java SSH API )使用文档

SSH, the Secure Shell is a popular network security protocol that defines a specification of how to conduct secure communication over an insecure network.This doc shows exactly how to use J2SSH API.What's more the api is open source.

ssh各种api

ssh框架的各种api,有标签库的,core库的,中文版和英文版,因上传大小有限制,就只上传这么多,都是chm格式的.

java使用jsch管理SFTP工具类

java使用jsch管理SFTP工具类 2017年09月01日 12:02:25 北岸灬 阅读数:3131 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22074635/article/details/77771395 maven坐标 <dependency> <groupId>com.jcraf...

f45056231p的博客 732

j2ssh-core-0.2.9.jar

j2ssh core 0 2 9 jar java模拟ssh协议登录

maverick2 java_J2SSH Maverick 一段连接sftp服务器,上传,下载的代码:

J2SSH MaverickVersion 1.2.6 一段连接sftp服务器,上传,下载的代码:import com.maverick.ssh.*;import com.maverick.ssh1.Ssh1Client;import com.maverick.ssh2.*;import java.io.*;import com.sshtools.net.*;import com.sshtools...

weixin_39816062的博客 426

SSH中文API帮助文档

SSH中文帮助文档 ...觉得很实用.拿出来.给大家分享..希望对大家有用

如何下载最新版本的VMware Workstation

随着虚拟化技术的发展,VMware Workstation作为行业领先的桌面虚拟化软件,为用户提供了在单一物理计算机上运行多个操作系统的能力。本文将详细介绍如何从Broadcom支持网站下载最新的VMware Workstation Pro版本。

安呀智数据坊的博客 690

通过SFTP协议连接服务器--J2SSH Maverick 简单使用

由于公司需要,在网上找了一下支持SFTP的包,发现俩个不错的,一个是 Secure FTP Factory ,一个是 J2SSH Maverick ,但都是收费的,现在简单介绍一下 J2SSH Maverick 怎样简单使用。代码如下: /* HEADER */ import com.maverick.ssh.*; import com.maverick.ssh2.*; import ...

airdream 1262

SAP MM委外加工保姆级教程:从BOM配置到MIGO发货的完整避坑指南

本文提供SAP MM模块中委外加工的完整操作指南,涵盖从BOM配置到MIGO发货的全流程。详细解析委外采购和工序委外两种模式,包括主数据配置、采购订单创建及物料移动等关键步骤,帮助用户规避常见错误,提升供应链管理效率。特别适合制造业企业实施SAP外包业务参考。

weixin_26905487的博客 538

有关b/s结构下applet在webServer中部署问题。。

问题: 有关b/s结构下applet在webServer中部署问题。。 webLogic 8.1jdk1.4.2Eclipse3.0.1Oracle9i主要功能是:通过applet遍历本地的文件,再applet上传到sftp服务器上,    原来的一套是applet遍历本地的文件,再applet上传到ftp服务器,用的是sun自带的包,整个系统都可以正常运行。    现在要改成上传到sftp服务器...

borderj的专栏 87

哈工大停用词表深度解析:从原理到实战应用与定制优化

在自然语言处理中,文本预处理是提升模型性能的关键步骤,其核心原理在于通过特征选择降低数据噪声、提升计算效率。停用词过滤作为预处理的重要技术,通过移除高频低信息词汇,能有效降低特征空间维度,让模型更聚焦于承载核心语义的实词,从而在文本分类、情感分析等任务中提升模型聚焦能力与相似度计算准确性。哈工大停用词表作为中文NLP领域的经典工具,基于大规模语料统计设计,覆盖了常见功能词与低信息实词,为工程实践提供了可靠基线。在实际应用中,需结合具体场景进行定制化调整,例如在情感分析中保留语气词与程度副词,在法律文本中保留

weixin_33738982的博客 491

vins-fusion环境配置、安装与测试

本文主要介绍如何搭建vins-fusion的运行环境,以及解决vins-fusion编译运行时遇到的环境冲突问题,并在此基础上实现例程的运行。

qq_52068373的博客 1万+

通过加j2ssh的方式访问linux服务器

/*** 通过ssh的方式连接linux服务器* @return opclient;* @throws Exception*/ public static SftpClient connectSsh(SshClient client,String zzbm){        SftpClient opclient = null ;     String user = "root";     ...

u011868434的博客 2655

VMware共享磁盘配置实战:从零搭建多机共享存储环境

本文详细介绍了在VMware环境中从零开始配置共享磁盘的完整实战流程。通过创建共享磁盘、挂载至多台虚拟机并修改.vmx配置文件等关键步骤,帮助用户搭建可用于数据库集群(如Oracle RAC)测试的多机共享存储环境,并提供了核心参数设置与常见问题排查指南。

weixin_29284201的博客 496

Java sftp上传文件 三种方法

最近工作中需要利用sftp上传文件,因为各种原因三种方法都试过了。找到个可以用。现在把我已经试用的三种方法简单的介绍下:一:jsch-0.1.54.jar  所支持的方法在linux与linux服务器之间用这个方法进行sftp交互,容易报错。对环境要求比较高,jdk1.8 以上,然后对两个服务器之间的ssh版本也有要求,有些版本下也容易出错,我在工作中遇到的情况是 ssh 6.6    5.1 都...

ab121121121121的博客 5201

《AgentScope-Java 深入浅出教程》第10章 RAG 知识检索

RAG(Retrieval-Augmented Generation,检索增强生成)是一种将信息检索与文本生成相结合的技术架构。它通过从外部知识库中检索相关文档,将检索到的信息作为上下文注入到语言模型的提示中,从而增强模型的回答质量。│ RAG 工作流程 ││ ││ 用户问题 向量检索 上下文增强 生成回答 ││ ▼ ││ │ A │ ││ ││ 1. 接收问题 ││ 2. 将问题转换为向量,在知识库中检索相似文档 ││ 3. 将检索到的文档作为上下文添加到提示词中 │。

步子哥的博客 472

解锁IPG Photonics SmartWeld系统,开启焊接新时代

SmartWeld 系统是 IPG Photonics 研发的一款先进的激光焊接解决方案,它隶属于自动化激光焊接系统类别 ,但却凭借一系列创新技术和独特设计,在众多同类产品中脱颖而出。该系统基于 IPG 在光纤激光技术领域的深厚积累,融合了先进的光束控制、智能监测和自动化控制等技术,旨在为工业制造提供高效、精准且稳定的焊接工艺。与传统激光焊接系统相比,SmartWeld 系统的独特之处首先体现在其光束控制技术上。它能够精确地调整激光束的能量分布、光斑大小和形状 ,以适应不同材料和焊接工艺的需求。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 1441
上一篇: 有关b/s结构下applet在webServer中部署问题。。
下一篇: Roller weblog 的中文问题,还发现JRoller不支持Unicode :-(
border1985
博客等级 码龄22年 5粉丝 42原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值