java sftp_Java SFTP 上传、下载等操作

Java SFTP 上传、下载等操作

实际开发中用到了 SFTP 用于交换批量数据文件,然后琢磨了下这方面的东西,基于 JSch 写了个工具类记录下,便于日后使用。

JSch是 SSH2 的纯Java实现。JSch 可以连接到sshd服务器并使用端口转发,X11转发,文件传输等,并且很方便的将其功能集成到Java程序中。

1、添加依赖

com.jcraft

jsch

0.1.55

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 listFiles(String sftpPath) throws SftpException {

Vector files = sftp.ls(sftpPath);

List result = new ArrayList();

Iterator iterator = files.iterator();

while (iterator.hasNext()) {

LsEntry isEntity = (LsEntry) iterator.next();

result.add(isEntity.getFilename());

}

return result;

}

}

在使用的的时候,需要调用 connect()开启连接,使用完后调用 disconnect() 关闭连接 。

本文主要用于个人记录笔记!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java进行SFTP下载,可以使用JSch工具包。JSch是Java Secure Channel的缩写,它是一个Java实现的可以完成SFTP上传下载的工具。可以将其集成到自己的应用程序中。 以下是使用JSch实现SFTP下载的步骤: 1. 配置SFTP服务器的相关信息,例如服务器的地址、端口、用户名和密码等。可以将这些信息保存在配置文件中。 2. 创建一个TransferObject对象,该对象保存了要下载的文件的基本信息,如本地文件路径、本地文件名和远程文件路径等。 3. 编写SFTP工具类,该类包含了SFTP下载的逻辑代码。可以使用JSch提供的ChannelSftp的get()方法来实现文件下载。 具体的代码实现可以参考上述引用中的相关内容。可以根据实际需求进行配置文件的读取、TransferObject对象的创建和SFTP工具类的编写。通过调用SFTP工具类中的方法,传入要下载的文件的信息即可完成Java SFTP下载操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java- SFTP文件上传下载](https://blog.csdn.net/weixin_44783506/article/details/129271770)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值