J2SSH Maverick 一段连接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.

j2ssh-core-0.2.9.jar

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

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

ssh各种api

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

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

多周期路径及set_multicycle_path详解

默认情况下,每条路径都被定义为单周期,即源触发器在时钟的任一边沿启动(launch)的数据都应该由目的触发器在时钟的下一上升沿捕获(capture)。 有的设计需要在数据被捕获前提供一些额外的周期,这类路径被称为多周期路径。 一、set_multicycle_path命令 命令格式如下: set_multicycle_path .........

Jackiezhang1993的博客 4万+

国内合规开通ChatGPT Plus实操指南:银行卡绑卡与支付全解析

ChatGPT Plus 是 OpenAI 官方推出的高级订阅服务,基于 GPT-4o 多模态大模型提供语音交互、图像理解与长上下文处理能力。其核心使用前提在于完成合规的国际支付绑定,而非网络接入方式。技术原理上,OpenAI 采用 Stripe 支付网关进行风控校验,重点匹配银行跨境权限、卡片类型(仅支持 Visa/Mastercard/Amex 信用卡)、BIN 号段及地理位置一致性。该机制决定了95%的绑卡失败源于银行端配置缺失或卡片不兼容,而非网络问题。实际应用中,PayPal 作为中间支付层可显著

chuannaoxuan4674的博客 2284

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

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

airdream 1262

Vivado 2021.1 实战:手把手教你用IEEE1735 V2加密你的Verilog源码(附完整TCL命令)

本文详细介绍了在Vivado 2021.1中使用IEEE1735 V2标准加密Verilog源码的全流程,包括许可证申请、密钥文件配置、TCL命令执行及加密后验证。通过实战步骤和完整TCL命令,帮助开发者有效保护FPGA设计知识产权,确保核心算法安全。

weixin_42706667的博客 202

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

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

borderj的专栏 88

DIY T12烙铁头驱动难题:用几个电容二极管,搞定NMOS上管驱动(附Multisim仿真)

本文详细介绍了如何利用电荷泵电路解决DIY T12烙铁头驱动中的NMOS上管驱动难题。通过巧妙组合电容和二极管,构建低成本高效的升压电路,实现栅极高压驱动需求。文章包含具体电路设计、元件选型建议、实际搭建注意事项以及Multisim仿真验证,为电子爱好者提供了一套完整的解决方案。

weixin_33640562的博客 342

从按下开机键到进入系统:深入主板“后台”,揭秘Super I/O芯片的上电时序与BIOS交互

本文深入探讨了Super I/O芯片(SIO)在电脑启动过程中的关键作用,详细解析了其架构设计、上电时序与BIOS交互机制。通过分析SIO的多功能集成控制器特性,包括低速I/O管理、硬件监控和电源管理,揭示了从按下电源按钮到系统初始化的精密硬件协作过程。文章还提供了常见问题的调试方法,为硬件工程师和嵌入式开发者提供了实用参考。

weixin_27014595的博客 142

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

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

u011868434的博客 2655

华为OD机试真题 新系统 2026-08-02 C++ 实现【战士技能规划设计】

你为一款动作游戏设计战士角色的技能。战士每个技能会消耗不同能量,释放技能有两个约束:连续释放技能数量不能超过 m 个,技能能量总和不能超过能量上限 k;如果超过则必须中断当前技能,进入调息状态,也就是分段。

MISAYAONE的博客 81

Java sftp上传文件 三种方法

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

ab121121121121的博客 5201

uboot引导vxworks6.9(T4240) 启动

vxworks内核配置下搜索RAM_LOW_ADRS, 值为0x01000000, 是vxworks下载启动的地址,uboot启动时会用到。vxworks6.9 t4240用的是32地址空间,uboot下用的是64位地址空间,去uboot下修改law物理地址空间为32位。vxworks下flash基地址为 0xff000000, 将vx下的地址修改为0xe0000000,和uboot下地址一致。uboot下flash基地址为0xe000000,vxworks内核配置为build-in 符号表。

珂珂可爱多 3252

YOLOv8多传感器融合技术——毫米波雷达融合技术详解

本文介绍了YOLOv8与毫米波雷达融合技术在自动驾驶系统中的应用。毫米波雷达具有全天候工作、测距测速精准等优势,但存在分辨率低等局限。通过融合YOLOv8的视觉检测能力,可显著提升系统在恶劣环境下的感知性能。文章详细阐述了融合实现方法,包括雷达数据处理、目标投影匹配、置信度融合等关键技术,并提供了评估指标和应用场景分析。这种多传感器融合方案能有效弥补单一传感器的不足,为自动驾驶提供更可靠的环境感知能力,是实现高级别自动驾驶的重要技术支撑。

u010117029的博客 178
上一篇: 有关b/s结构下applet在webServer中部署问题。。
下一篇: Roller weblog 的中文问题,还发现JRoller不支持Unicode :-(
borderj
博客等级 码龄20年 7粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值