使用JSch将文件放入远程目录,如果该目录不存在,则创建它 [英] Use JSch to put a file to the remote directory and if the directory does not exist, then create it

查看:255
本文介绍了使用JSch将文件放入远程目录,如果该目录不存在,则创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Jsch库和SFTP协议将文件复制到远程目录。
如果远程主机上的目录不存在,则创建它。

I would like to copy a file to the remote directory using Jsch library and SFTP protocol. If the directory on the remote host does not exist, then create it.

在API文档中, http://epaul.github.com/jsch-documentation/javadoc/ ,我在
put方法中注意到那里是一种模式,但它只是转移模式:
- 传输模式,RESUME,APPEND,OVERWRITE之一。

In the API doc, http://epaul.github.com/jsch-documentation/javadoc/, I noticed in the put method that there is a kind of "mode" but it is just the transfer mode: - the transfer mode, one of RESUME, APPEND, OVERWRITE.

有没有一种简单的方法可以做到这一点,而无需编写我自己的代码来检查是否存在
,然后递归创建一个目录?

Is there an easy way to do this without having to write my own code to check the existence and then create a directory recursively?

推荐答案

据我所知。我使用以下代码来实现同样的目的:

Not as far as I know. I use the following code to achieve the same thing:

String[] folders = path.split( "/" );
for ( String folder : folders ) {
    if ( folder.length() > 0 ) {
        try {
            sftp.cd( folder );
        }
        catch ( SftpException e ) {
            sftp.mkdir( folder );
            sftp.cd( folder );
        }
    }
}

其中 sftp ChannelSftp 对象。

这篇关于使用JSch将文件放入远程目录,如果该目录不存在,则创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆