Java远程连接服务器实现文件上传下载及目录操作
详情请阅读原文
在其基础之上做了进一步的封装
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
import com.jcraft.jsch.*;
import java.util.Properties;
public class SFTPChannel {
//ip地址
private String host;
//登录账号
private String username;
//登录密码
private String password;
//默认端口号
private int port = 22;
//默认过期时间
private int timeout = 60000;
private Session session = null;
private Channel channel = null;
public SFTPChannel(String host, String username, String password) {
this.host = host;
this.username = username;
this.password = password;
}
public SFTPChannel(String host, String username, String password, int port) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
public SFTPChannel(String host, String username, String password, int port, int timeout) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
this.timeout = timeout;
}
//创建连接
public ChannelSftp getChannel(SFTPChannel sftpChannel) throws JSchException {
// 创建JSch对象
JSch jSch = new JSch();
// 根据用户名,主机ip,端口获取一个Session对象
session = jSch.getSession(sftpChannel.getUsername(), sftpChannel.getHost(), sftpChannel.getPort());
// 设置密码
session.setPassword(sftpChannel.getPassword());
Properties properties = new Properties();
//主机公钥确认 无口令 SSH 登录(即通过客户端公钥认证),就可以直接连接到远程主机。
//这是基于 SSH 协议的自动化任务常用的手段
properties.put("StrictHostKeyChecking", "no");
// 为Session对象设置properties
session.setConfig(properties);
// 设置timeout时间
session.setTimeout(sftpChannel.getTimeout());
// 通过Session建立链接
session.connect();
// 打开SFTP通道
channel = session.openChannel("sftp");
// 建立SFTP通道的连接
channel.connect();
return (ChannelSftp) channel;
}
//关闭连接
public void closeChannel() {
if (null != channel) {
channel.disconnect();
}
if (null != session) {
session.disconnect();
}
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
使用示例
public static void main(String[] args) throws Exception {
SFTPChannel sftpChannel = new SFTPChannel("服务器ip", "root", "root");
//直接将本地文件名为src的文件上传到目标服务器,目标文件名为dst。
//(注:使用这个方法时,dst可以是目录,当dst是目录时,上传后的目标文件名将与src文件名相同)
String src = "D:\\project\\Rar.txt";
String dst = "/opt/csv/";
ChannelSftp channelSftp = sftpChannel.getChannel(sftpChannel);
System.out.println("创建链接");
channelSftp.put(src, dst, ChannelSftp.OVERWRITE);
System.out.println("上传文件成功");
//展示上传文件目录下的所有文件
Vector vector = channelSftp.ls(dst);
System.out.println(vector.toString());
//关闭连接
channelSftp.quit();
sftpChannel.closeChannel();
}
其他目录文件操作请看原文
Java远程连接服务器实现文件上传下载及目录操作的更多相关文章
- Java实现FTP批量大文件上传下载篇1
本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...
- xftp实现本地与服务器的文件上传下载(windows)
背景: Jemter环境搭建,需上传下载服务器文件到aws服务器上,由于secureCRT的局限性它只支持pub格式的密钥,不支持pem格式密钥,xshell是支持pem格式的,所以尝试安装xshel ...
- Java实现FTP与SFTP文件上传下载
添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...
- JAVA中使用FTPClient实现文件上传下载
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- linux命令行模式下对FTP服务器进行文件上传下载
参考源:点击这里查看 1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码 ...
- JAVA中使用FTPClient实现文件上传下载实例代码
一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- 基于 java 【Web安全】文件上传漏洞及目录遍历攻击
前言:web安全之文件上传漏洞,顺带讲一下目录遍历攻击.本文基于 java 写了一个示例. 原理 在上网的过程中,我们经常会将一些如图片.压缩包之类的文件上传到远端服务器进行保存.文件上传攻击指的是恶 ...
- java中io流实现文件上传下载
新建io.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
- vb.net FTP上传下载,目录操作
https://blog.csdn.net/dzweather/article/details/51429107 FtpWebRequest与FtpWebResponse类用来与特定FTP服务器进行沟 ...
- Android与Asp.Net Web服务器的文件上传下载BUG汇总[更新]
遇到的问题: 1.java.io.IOException: open failed: EINVAL (Invalid argument)异常,在模拟器中的sd卡创建文件夹和文件时报错 出错原因可能是: ...
随机推荐
- scanf、cin及其优化、快读性能测试
为了让大家了解C++各种IO方式的性能,于是就有了这篇文章. 本次测试采取的数据均为 \(10^6\) 个不超过 \(10^8\) 随机正整数. 测试代码: #include<bits/stdc ...
- 微服务:gateway
网关路由: 1.创建新模块 2.引入网关依赖 <!--网关--> <dependency> <groupId>org.springframework.cloud&l ...
- Diffutoon下载介绍:真人视频转动漫工具,轻松获得上千点赞
最近在刷短视频的时候,偶尔能看到一些真人转动漫风的作品,看起来给人一种新鲜感,流量都还不错,简简单单跳个舞,就能获得上千个点赞~ 那么,这种视频是怎么制作的? 本期给大家介绍一款AI转绘工具Diffu ...
- 自制基于simplefoc大功率驱动板想法的由来,同时欢迎有相同兴趣的F友一起来玩。。。
前一阵子,偶然在B站上看了一个simplefoc的介绍视频,代码简洁.算法精妙让人佩服,更让人佩服的是:开源!遂!搜索之!不搜不知道一搜吓一跳,发现太OUT了,原来玩这个算法的人这么多,让我这个整天沉 ...
- android 点击退出按钮 结束所有的activity 回到手机主页面
android 点击退出按钮 结束所有的activity 回到手机主页面我 实现了回到主页面 但是在点击这个程序 他还是回到**退出的界面 ** 我要实现点击按钮回到手机主页面 在点击这个程序后就重新 ...
- Jmeter函数助手27-urlencode
urlencode函数用于将字符串进行application/x-www-form-urlencoded编码格式化. String to encode in URL encoded chars:填入字 ...
- 【ECharts】04 数据交互
ECharts 异步加载数据 ECharts 通常数据设置在 setOption 中,如果我们需要异步加载数据,可以配合 jQuery等工具,在异步获取数据后通过 setOption 填入数据和配置项 ...
- 【Vue】13 VueRouter Part3 路由守卫
单页应用中,只存在一个HTML文件,网页的标签,是通过title标签显示的,我们在单页应用中如何修改? JS操作: window.document.title = "标签名称" 也 ...
- LeetCode 上1769号 面试编程题,python编程
原题地址: https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/ - ...
- ubuntu20.04/22.04 系统环境下源码编译Python3.10
2022年10月3日更新 在Ubuntu22.04系统上源码编译python,所依赖环境的安装命令为: sudo apt install gcc g++ libffi-dev build-essent ...