Java SFTP 上传、下载等操作
Java SFTP 上传、下载等操作
实际开发中用到了 SFTP
用于交换批量数据文件,然后琢磨了下这方面的东西,基于 JSch
写了个工具类记录下,便于日后使用。
JSch
是 SSH2
的纯Java实现。JSch
可以连接到sshd服务器并使用端口转发,X11转发,文件传输等,并且很方便的将其功能集成到Java程序中。
1、添加依赖
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2、SFTPUtils 工具类
public class SFTPUtils {
private Logger log = LoggerFactory.getLogger(SFTPUtils.class);
private String host; // 主机名称/IP
private int port = 22; // 端口
private String username; // 用户名
private String password; // 密码
private ChannelSftp sftp = null;
private Channel channel = null;
private Session session = null;
public SFTPUtils(String host, String userName, String password) {
this.host = host;
this.username = userName;
this.password = password;
}
public SFTPUtils(String host, int port, String userName, String password) {
this.host = host;
this.port = port;
this.username = userName;
this.password = password;
}
/**
* 连接SFTP服务器
*
* @throws JSchException
*/
public void connect() throws JSchException {
JSch jSch = new JSch();
session = jSch.getSession(username, host, port);
session.setPassword(password);
session.setConfig(this.buildConfig());
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("连接主机:{} 登录成功", host);
}
/**
* 构建连接配置参数
*
* @return Properties
*/
private Properties buildConfig() {
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
return properties;
}
/**
* 关闭连接
*/
public void disconnect() {
try {
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
} catch (Throwable e) {
//ignore
}
}
/**
* 下载文件
*
* @param sftpPath 服务器路径,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。
* 例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;
* 指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。
* @param fileName 文件名
* @param toFilePath 下载保存文件路径,路径+文件名,例如:d:/test.txt
* @return
*/
public boolean downloadFile(String sftpPath, String fileName, String toFilePath) {
FileOutputStream fileOutputStream = null;
try {
if (StringUtils.isNotBlank(sftpPath)) {
sftp.cd(sftpPath);
}
fileOutputStream = new FileOutputStream(new File(toFilePath));
sftp.get(fileName, fileOutputStream);
return true;
} catch (Exception e) {
log.error("下载文件错误", e);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
//ignore
}
}
}
return false;
}
/**
* 上传文件
*
* @param sftpPath 服务器路径,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。
* 例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;
* 指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。
* @param fileName 上传后文件名
* @param localFilePath 文件路径,路径+文件名,例如:d:/test.txt
* @return
*/
public boolean uploadFile(String sftpPath, String fileName, String localFilePath) {
FileInputStream inputStream = null;
try {
if (StringUtils.isNotBlank(sftpPath)) {
sftp.cd(sftpPath);
}
inputStream = new FileInputStream(new File(localFilePath));
sftp.put(inputStream, fileName);
return true;
} catch (Exception e) {
log.error("上传文件错误", e);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
//ignore
}
}
}
return false;
}
/**
* 上传文件
*
* @param sftpPath 服务器路径,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。
* 例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;
* 指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。
* @param fileName 上传后文件名
* @param inputStream 文件输入流
* @return
*/
public boolean uploadFile(String sftpPath, String fileName, InputStream inputStream) {
try {
if (StringUtils.isNotBlank(sftpPath)) {
sftp.cd(sftpPath);
}
sftp.put(inputStream, fileName);
return true;
} catch (Exception e) {
log.error("上传文件错误", e);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
//ignore
}
}
}
return false;
}
/**
* 删除文件
*
* @param sftpPath 服务器路径,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。
* 例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;
* 指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。
* @param fileName 文件名
* @return
*/
public boolean deleteFile(String sftpPath, String fileName) {
try {
if (StringUtils.isNotBlank(sftpPath)) {
sftp.cd(sftpPath);
}
sftp.rm(fileName);
return true;
} catch (Exception e) {
log.error("删除文件失败", e);
}
return false;
}
/**
* 查询指定目录下信息
*
* @param sftpPath 服务器路径,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。
* 例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;
* 指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。
* @return
*/
public List<String> listFiles(String sftpPath) throws SftpException {
Vector files = sftp.ls(sftpPath);
List<String> result = new ArrayList<String>();
Iterator iterator = files.iterator();
while (iterator.hasNext()) {
LsEntry isEntity = (LsEntry) iterator.next();
result.add(isEntity.getFilename());
}
return result;
}
}
在使用的的时候,需要调用 connect()
开启连接,使用完后调用 disconnect()
关闭连接 。
jsch
官方的文档说明 http://www.jcraft.com/jsch/
本文主要用于个人记录笔记!
Java SFTP 上传、下载等操作的更多相关文章
- Java Sftp上传下载文件
需要使用jar包 jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import ja ...
- JAVA Sftp 上传下载
SftpUtils package xxx;import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com ...
- java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)
1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...
- SFTP上传下载文件、文件夹常用操作
SFTP上传下载文件.文件夹常用操作 1.查看上传下载目录lpwd 2.改变上传和下载的目录(例如D盘):lcd d:/ 3.查看当前路径pwd 4.下载文件(例如我要将服务器上tomcat的日志文 ...
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
- Xshell5下利用sftp上传下载传输文件
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...
- THINKPHP 3.2 PHP SFTP上传下载 代码实现方法
一.SFTP介绍:使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议.区别:sftp是ssh内含的协议(ssh是加密的telnet协议), 只要sshd服 ...
- java 实现Serv-U FTP 和 SFTP 上传 下载
两种ftp使用java的实现方式 ,代码都已测试 第一种:Serv-U FTP 先决条件: 1.Serv-U FTP服务器搭建成功. 2.jar包需要:版本不限制 <!--ftp上传需要的jar ...
- java实操之使用jcraft进行sftp上传下载文件
sftp作为临时的文件存储位置,在某些场合还是有其应景的,比如对账文件存放.需要提供一个上传的工具类.实现方法参考下: pom.xml中引入类库: <dependency> <gro ...
随机推荐
- Solon详解(九)- 渲染控制之定制统一的接口输出
Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...
- osgEarth使用笔记4——加载矢量数据
目录 1. 概述 2. 详论 2.1. 基本绘制 2.2. 矢量符号化 2.2.1. 可见性 2.2.2. 高度设置 2.2.3. 符号化 2.2.4. 显示标注 2.3. 其他 3. 结果 4. 问 ...
- 借助C++探究素数的分布
这里使用的区间是36,144,576,2304,9216,36864,147456,589824,2359296,9437184.至于这个区间是怎么得到的,感兴趣的同鞋可前往(https://www. ...
- python排序算法总结和实现
------------------希尔排序------------- 一直没搞懂希尔排序怎么搞得 def Shell_sort(L): step = len(L)/2 while step > ...
- Python基础-列表、元组、字典、字符串(精简解析)
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- ATMEGA的SPI总线 - 第2部分
参考: 1.https://www.yiboard.com/thread-783-1-1.html 2.https://mansfield-devine.com/speculatrix/2018/01 ...
- kalilinux2020.3的安装与一些坑
1.下载镜像文件.iso kali官方下载太慢,用一些魔法也是不行,这里推荐用国内的下载源. 阿里云: https://mirrors.aliyun.com/kali-images/?spm=a2c6 ...
- java安全编码指南之:输入注入injection
目录 简介 SQL注入 java中的SQL注入 使用PreparedStatement XML中的SQL注入 XML注入的java代码 简介 注入问题是安全中一个非常常见的问题,今天我们来探讨一下ja ...
- 记录小坑-tp5 使用模型select查询
场景: 使用模型去select查询后进行业务处理 再进行 saveAll 提示缺少更新条件 坑点:此时取出的数据结构是 query对象 { array:[ xxxx => xxx ] }: sa ...
- 多测师讲解_python_pycharm基本实用操作__保存代码_
pycharm中中保存代码的方式: 方式一: 方式二: 第一步: 第二步: