如何在使用JSCH SFTP API创建新目录之前检查目录的存在?我正在尝试使用
lstat
,但不确定它是否可以完成我需要的工作。先行致谢。这是我在JSch中检查目录是否存在的方法。
如果目录不存在,则创建该目录。
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);
}
我在这里以更广泛的背景重复相同的答案。检查目录是否存在并创建新目录的特定代码行是
SftpATTRS attrs;
try {
attrs = channel.stat(localChildFile.getName());
}catch (Exception e) {
channel.mkdir(localChildFile.getName());
}
注意: localChildFile.getName()
是您要检查的目录名称。下面附上整个类,该类会递归地将文件或目录内容发送到远程服务器。
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* Created by krishna on 29/03/2016.
*/
public class SftpLoader {
private static Logger log = LoggerFactory.getLogger(SftpLoader.class.getName());
ChannelSftp channel;
String host;
int port;
String userName ;
String password ;
String privateKey ;
public SftpLoader(String host, int port, String userName, String password, String privateKey) throws JSchException {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
this.privateKey = privateKey;
channel = connect();
}
private ChannelSftp connect() throws JSchException {
log.trace("connecting ...");
JSch jsch = new JSch();
Session session = jsch.getSession(userName,host,port);
session.setPassword(password);
jsch.addIdentity(privateKey);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
log.trace("connected !!!");
return (ChannelSftp)channel;
}
public void transferDirToRemote(String localDir, String remoteDir) throws SftpException, FileNotFoundException {
log.trace("local dir: " + localDir + ", remote dir: " + remoteDir);
File localFile = new File(localDir);
channel.cd(remoteDir);
// for each file in local dir
for (File localChildFile: localFile.listFiles()) {
// if file is not dir copy file
if (localChildFile.isFile()) {
log.trace("file : " + localChildFile.getName());
transferFileToRemote(localChildFile.getAbsolutePath(),remoteDir);
} // if file is dir
else if(localChildFile.isDirectory()) {
// mkdir the remote
SftpATTRS attrs;
try {
attrs = channel.stat(localChildFile.getName());
}catch (Exception e) {
channel.mkdir(localChildFile.getName());
}
log.trace("dir: " + localChildFile.getName());
// repeat (recursive)
transferDirToRemote(localChildFile.getAbsolutePath(), remoteDir + "/" + localChildFile.getName());
channel.cd("..");
}
}
}
public void transferFileToRemote(String localFile, String remoteDir) throws SftpException, FileNotFoundException {
channel.cd(remoteDir);
channel.put(new FileInputStream(new File(localFile)), new File(localFile).getName(), ChannelSftp.OVERWRITE);
}
public void transferToLocal(String remoteDir, String remoteFile, String localDir) throws SftpException, IOException {
channel.cd(remoteDir);
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(channel.get(remoteFile));
File newFile = new File(localDir);
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
log.trace("writing files ...");
int readCount;
while( (readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
log.trace("completed !!!");
bis.close();
bos.close();
}
attrs
未找到目录时,它不会为空,而是会抛出异常。 - SRyelse
代码块,而且没有机会控制转到if-else
代码块。因此,你的代码只有在attrs
不为空时才能工作。 - SRytry { channel.stat(folderName); } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { channel.mkdir(folderName); } else { throw e; } }
- Ton Bosma