本文整理匯總了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");
}
}