本文整理汇总了Java中net.schmizz.sshj.SSHClient.newSFTPClient方法的典型用法代码示例。如果您正苦于以下问题:Java SSHClient.newSFTPClient方法的具体用法?Java SSHClient.newSFTPClient怎么用?Java SSHClient.newSFTPClient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.SSHClient
的用法示例。
在下文中一共展示了SSHClient.newSFTPClient方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadFile
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void uploadFile() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
File testFile = local.newFile("fred.txt");
try {
sftp.put(new FileSystemFile(testFile), "/fred.txt");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(sftpHome.getRoot().list(), is(new String[]{"fred.txt"}));
}
示例2: transferEvents
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private void transferEvents(SSHClient sshClient, List<MotionEvent> motionEvents) throws IOException, ParseException {
for (MotionEvent motionEvent : motionEvents) {
ByteArraySourceFile byteArraySourceFile = new ByteArraySourceFile.Builder()
.fileData(motionEvent.getImage())
.name(motionEvent.getId() + ".jpg")
.build();
String day = concurrentDateFormatAccess.convertDateToString(motionEvent.getTimestamp());
SFTPClient sftpClient = sshClient.newSFTPClient();
String destinationPath = eyeballsConfiguration.getSftpDestinationDirectory() + "/" + day + "/";
sftpClient.mkdirs(destinationPath);
sftpClient.put(byteArraySourceFile, destinationPath);
}
}
示例3: downloadFile
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void downloadFile() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
FileUtils.write(sftpHome.newFile("fred.txt"), "Electric boogaloo");
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.get("fred.txt", new FileSystemFile(new File(local.getRoot(), "fred.txt")));
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(FileUtils.readFileToString(new File(local.getRoot(), "fred.txt")),
is("Electric boogaloo"));
}
示例4: getTempLocalInstance
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public File getTempLocalInstance( String remoteFilePath )
{
File tempFile = null;
SSHClient client = new SSHClient();
// required if host is not in knownLocalHosts
client.addHostKeyVerifier(new PromiscuousVerifier());
try {
client.connect(hostName);
client.authPassword(userName, userPass);
tempFile = File.createTempFile("tmp", null, null);
tempFile.deleteOnExit();
SFTPClient sftp = client.newSFTPClient();
sftp.get(remoteFilePath, new FileSystemFile(tempFile));
sftp.close();
client.disconnect();
} catch( Exception e ) {
//TODO: this needs to be more robust than just catching all exceptions
e.printStackTrace();
logger.error( e.toString() );
return null;
}
return tempFile;
}
示例5: listFiles
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void listFiles() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
sftpHome.newFile("fred.txt");
List<RemoteResourceInfo> files;
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
try {
files = sftp.ls("/");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(files.size(), is(1));
assertThat(files.get(0).getName(), is("fred.txt"));
}
示例6: listFilesUsingNativeSystem
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Override
protected Set<FileInfo> listFilesUsingNativeSystem(LogAccessConfig logAccessConfig, String subPath) throws LogAccessException {
// Get ssh client
SSHClient sshClient = sshClientThreadLocal.get();
// Define target directory
String targetPath = logAccessConfig.getDirectory();
if (subPath != null) {
targetPath += "/" + subPath;
}
// List files and directories (keep only the 'fileListMaxCount' last modified resources)
SFTPClient sftpClient = null;
Collection<RemoteResourceInfo> remoteResourceInfos;
try {
sftpClient = sshClient.newSFTPClient();
LastUpdatedRemoteResourceFilter remoteResourcefilter = new LastUpdatedRemoteResourceFilter(configService.getFileListMaxCount());
sftpClient.ls(targetPath, remoteResourcefilter);
remoteResourceInfos = remoteResourcefilter.getRemoteResourceInfos();
}
catch (IOException e) {
throw new LogAccessException("Error when listing files and directories on " + logAccessConfig, e);
}
finally {
IOUtils.closeQuietly(sftpClient, sshClient);
}
// Extract meta-informations
Set<FileInfo> fileInfos = new TreeSet<FileInfo>();
for (RemoteResourceInfo remoteResourceInfo : remoteResourceInfos) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(remoteResourceInfo.getName());
fileInfo.setRelativePath(remoteResourceInfo.getPath().substring(logAccessConfig.getDirectory().length() + 1).replace('\\', '/'));
fileInfo.setDirectory(remoteResourceInfo.isDirectory());
fileInfo.setLastModified(new Date(remoteResourceInfo.getAttributes().getMtime() * 1000L));
fileInfo.setFileSize(remoteResourceInfo.isDirectory() ? 0L : remoteResourceInfo.getAttributes().getSize());
fileInfo.setLogAccessType(LogAccessType.SSH);
fileInfos.add(fileInfo);
}
// Return meta-informations about files and folders
return fileInfos;
}
示例7: mkdirs
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
/**
* Create directories in environment if they don't exist. If directoryPath consists of several
* directories all required parent directories are created as well.
*
* @param directoryPath path to create
*/
public void mkdirs(String directoryPath) throws IOException {
LOGGER.trace("mkdirs({})", directoryPath);
String finalDirectoryPath = concatenatePathToHome(directoryPath);
FilenameUtils.normalize(finalDirectoryPath, true);
if (!finalDirectoryPath.startsWith("/")) {
// SFTP mkdirs requires to a dot for relative path, otherwise it assumes given path is
// absolute.
finalDirectoryPath = "./" + finalDirectoryPath;
}
SSHClient ssh = connectWithSSH();
try {
try {
try (SFTPClient sftp = ssh.newSFTPClient()) {
sftp.mkdirs(finalDirectoryPath);
}
} catch (IOException e) {
// SFTP has failed (on some systems it may be just disabled) revert to mkdir.
List<String> command = new ArrayList<>();
command.add("mkdir");
command.add("-p");
command.add(finalDirectoryPath);
String sshHomeDir = ".";
int exitStatus = runCommand(command, ssh, sshHomeDir);
if (exitStatus != 0) {
throw new IOException("Could not create suggested directories.");
}
}
} finally {
ssh.disconnect();
}
}
示例8: client
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public SFTPClient client() throws IOException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PublicKeyVerifier(hostKey.publicKey()));
ssh.connect("localhost", port);
createdClients.add(ssh);
if (null == password) {
ssh.authPublickey(user, new KeyPairWrapper(keyPair));
} else {
ssh.authPassword(user, password);
}
return ssh.newSFTPClient();
}
示例9: getFilesInPath
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public ArrayList< String > getFilesInPath( String path )
{
ArrayList< String > ret = new ArrayList< String >();
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
try {
client.connect(hostName);
client.authPassword(userName, userPass);
SFTPClient sftp = client.newSFTPClient();
List< RemoteResourceInfo > files = sftp.ls( path );
for( RemoteResourceInfo file: files )
{
ret.add( file.getPath() );
}
sftp.close();
client.disconnect();
} catch( Exception e ) {
logger.error( e.toString() );
}
return ret;
}