本文整理汇总了Java中org.apache.sshd.client.SftpClient类的典型用法代码示例。如果您正苦于以下问题:Java SftpClient类的具体用法?Java SftpClient怎么用?Java SftpClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SftpClient类属于org.apache.sshd.client包,在下文中一共展示了SftpClient类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyBootStrap
import org.apache.sshd.client.SftpClient; //导入依赖的package包/类
protected String copyBootStrap(ClientSession session) throws IOException {
String dest = BOOTSTRAP_FILE.get() + random.nextLong();
SftpClient sftpClient = session.createSftpClient();
try {
Handle handle = sftpClient.open(dest, EnumSet.of(OpenMode.Read, OpenMode.Write, OpenMode.Create, OpenMode.Exclusive));
Attributes attr = new Attributes().perms(0700);
sftpClient.setStat(handle, attr);
byte[] content = getBootstrapSource(BOOTSTRAP_SOURCE_OVERRIDE.get(), BOOTSTRAP_SOURCE.get());
sftpClient.write(handle, 0, content, 0, content.length);
sftpClient.close(handle);
return dest;
} finally {
if ( sftpClient != null ) {
sftpClient.close();
}
}
}
示例2: copyBootStrap
import org.apache.sshd.client.SftpClient; //导入依赖的package包/类
protected String copyBootStrap(ClientSession session) throws IOException {
String dest = BOOTSTRAP_FILE.get() + random.nextLong();
SftpClient sftpClient = session.createSftpClient();
try {
Handle handle = sftpClient.open(dest, EnumSet.of(OpenMode.Read, OpenMode.Write, OpenMode.Create, OpenMode.Exclusive));
Attributes attr = new Attributes().perms(0700);
sftpClient.setStat(handle, attr);
byte[] content = BootstrapScript.getBootStrapSource();
sftpClient.write(handle, 0, content, 0, content.length);
sftpClient.close(handle);
return dest;
} finally {
if ( sftpClient != null ) {
sftpClient.close();
}
}
}
示例3: receiveFiles
import org.apache.sshd.client.SftpClient; //导入依赖的package包/类
@Override
public void receiveFiles(TransferJob job) throws FileNotFoundException, Exception {
log.debug("Receiving files from {}",job.getSourceUrl());
// TODO error handle URI parsing.
final URI url = new URI(job.getSourceUrl());
final String hostname = url.getHost();
final int port = url.getPort() != -1 ? url.getPort() : 22;
final String path = url.getPath();
ConnectFuture connectFuture = sshClient.connect(job.getSourceUsername(), hostname, port);
ClientSession session = connectFuture.await().getSession();
session.addPasswordIdentity(job.getSourcePassword());
session.auth().await();
SftpClient sftpClient = session.createSftpClient();
for(DirEntry dirEntry : sftpClient.readDir(path)){
if( dirEntry.attributes.isRegularFile()
&& FilenameUtils.wildcardMatch(dirEntry.filename, job.getSourceFilepattern())){
queueFile(dirEntry,path,sftpClient,job);
}
}
sftpClient.close();
session.close(false);
}
示例4: sendFile
import org.apache.sshd.client.SftpClient; //导入依赖的package包/类
@Override
public void sendFile(Message msg, TransferJob job, TransferEvent event) {
try {
final String filename = msg.getStringProperty("filename");
final URI url = new URI(job.getTargetUrl());
final String hostname = url.getHost();
final int port = url.getPort() != -1 ? url.getPort() : 22;
final String path = url.getPath();
ConnectFuture connectFuture = sshClient.connect(job.getTargetUsername(), hostname, port);
ClientSession session = connectFuture.await().getSession();
session.addPasswordIdentity(job.getTargetPassword());
session.auth().await();
SftpClient sftpClient = session.createSftpClient();
// TODO make it possible to rename the file according to some generic pattern.
//job.getTargetFilename() + RandomStringUtils.randomAlphanumeric(5);
log.debug("Saving file to SFTP: {}, job: {}", hostname + ":" + port + path + "/" + filename, job.getName() + "(" + job.getId() + ")");
OutputStream os = sftpClient.write(path + "/" + filename);
BufferedOutputStream bos = new BufferedOutputStream(os);
// This will block until the entire content is saved on disk
msg.setObjectProperty("JMS_AMQ_SaveStream", bos);
bos.close();
os.close();
event.setState("done");
event.setTimestamp(new DateTime());
event = eventRepo.saveAndFlush(event);
log.debug("File saved to SFTP: {}, job: {}", path + "/" + filename, job.getName() + "(" + job.getId() + ")");
} catch (Exception e) {
event.setState("send failed");
event.setTimestamp(new DateTime());
event = eventRepo.save(event);
log.warn("Error sending file {}, job: {}", event.getFilename(), job.getName() + "(" + job.getId() + ")");
log.warn("Error descr", e);
throw new RuntimeException("Rollback SFTP transaction");
}
}