ls
JSchを使用して、名前の一致を見つけるためにファイルをループして実行せずに、リモートファイルが存在するかどうかを確認する方法はありますか?
ありがとう
次のようなこともできます。
try {
channelSftp.lstat(name);
} catch (SftpException e){
if(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
// file doesn't exist
} else {
// something else went wrong
throw e;
}
}
存在しないものに対してlstatを実行すると、IDが2のSftpExecptionが取得されます。それ以外の場合は、ファイルに関する情報が取得されます。
これは、 JSchでディレクトリの存在を確認する方法です。
注:この質問とは関係ありませんが、役立つ場合があります。
dirが存在しない場合はディレクトリを作成します
ChannelSftp channelSftp = (ChannelSftp)channel;
String currentDirectory=channelSftp.pwd();
String dir="abc";
SftpATTRS attrs=null;
try {
attrs = channelSftp.stat(currentDirectory+"/"+dir);
} catch (Exception e) {
System.out.println(currentDirectory+"/"+dir+" not found");
}
if (attrs != null) {
System.out.println("Directory exists IsDir="+attrs.isDir());
} else {
System.out.println("Creating dir "+dir);
channelSftp.mkdir(dir);
}
実際、私のプロジェクトls
ではループなしで動作しています。ls
ファイル名を使用して呼び出しパスに渡すだけです。
private static boolean exists(ChannelSftp channelSftp, String path) {
Vector res = null;
try {
res = channelSftp.ls(path);
} catch (SftpException e) {
if (e.id == SSH_FX_NO_SUCH_FILE) {
return false;
}
log.error("Unexpected exception during ls files on sftp: [{}:{}]", e.id, e.getMessage());
}
return res != null && !res.isEmpty();
}
たとえばfile.txt
、URLを持つファイルがありますsftp://user@www.server.comm/path/to/some/random/folder/file.txt
。exists
path
私はとして機能するために渡します/path/to/some/random/folder/file.txt
(これは、ライブラリのSFTP部分を使用している場合であり、私がそれについて考えずに行った仮定です。)
私はそれls(String path)
がファイル名を受け入れるだろうと思った。現時点では確認できません。
そうでない場合は、手動で繰り返す必要はありません。セレクターバリアントを使用できます。
ls(String path, ChannelSftp.LsEntrySelector selector)
import java.util.ArrayList;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FileExists {
ChannelExec channelExec = null;
static Channel channel = null;
static String host = "hostname";
static String user = "username";
static String password = "password$";
public static void main(String[] args) {
String filename = "abc.txt";
String filepath = "/home/toolinst/ggourav";
try {
Channel channel = getChannelSftp(host, user, password);
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd(filepath);
String path = channelSftp.ls(filename).toString();
if (!path.contains(filename)) {
System.out.println("File doesn't exist.");
} else
System.out.println("File already exist.");
} catch (Exception e) {
e.printStackTrace();
}
}
private static Channel getChannelSftp(String host, String user, String password) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(config);
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
} catch (Exception e) {
System.out.println("Failed to get sftp channel. " + e);
}
return channel;
}
}
あなたはによってチェックすることができます
if [ -e FILE_NAME ] ; then
//do something
fi
また
if [ -d DIRNAME ]
ディレクトリ用
また
if [ -l SYMLINK ]
ソフトリンク用
これがお役に立てば幸いです
リモートマシンでコマンドを実行する例を次に示しますhttp://www.jcraft.com/jsch/examples/Exec.java.html
スクリプト全体を非常にうまく実行ls
または渡すことができます。スクリプトをリモートマシンにコピーして実行するのと同じです。