当前位置: 首页>>代码示例>>Java>>正文


Java SFTPClient类代码示例

本文整理汇总了Java中net.schmizz.sshj.sftp.SFTPClient的典型用法代码示例。如果您正苦于以下问题:Java SFTPClient类的具体用法?Java SFTPClient怎么用?Java SFTPClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SFTPClient类属于net.schmizz.sshj.sftp包,在下文中一共展示了SFTPClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: uploadFile

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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"}));
}
 
开发者ID:mlk,项目名称:AssortmentOfJUnitRules,代码行数:24,代码来源:TestSftpRule.java

示例2: transferEvents

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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);
    }
}
 
开发者ID:chriskearney,项目名称:eyeballs,代码行数:14,代码来源:SftpMotionEventConsumer.java

示例3: lastModified

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
public long lastModified() throws MalformedURLException, SmbException {
    switch (mode) {
        case SFTP:
            SshClientUtils.execute(new SFtpClientTemplate(path) {
                @Override
                public Long execute(SFTPClient client) throws IOException {
                    return client.mtime(SshClientUtils.extractRemotePathFrom(path));
                }
            });
            break;
        case SMB:
            SmbFile smbFile = getSmbFile();
            if (smbFile != null)
                return smbFile.lastModified();
            break;
        case FILE:
            new File(path).lastModified();
            break;
        case ROOT:
            HybridFileParcelable baseFile = generateBaseFileFromParent();
            if (baseFile != null)
                return baseFile.getDate();
    }
    return new File("/").lastModified();
}
 
开发者ID:TeamAmaze,项目名称:AmazeFileManager,代码行数:26,代码来源:HybridFile.java

示例4: downloadFile

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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"));
}
 
开发者ID:mlk,项目名称:AssortmentOfJUnitRules,代码行数:27,代码来源:TestSftpRule.java

示例5: copyFilesFromLocalDiskShouldRunSCPAndToCorrectDirectory

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Test
public void copyFilesFromLocalDiskShouldRunSCPAndToCorrectDirectory() throws IOException {
  SCPFileTransfer mockSCPFileTransfer = mock(SCPFileTransfer.class);
  when(mMockSSHClient.newSCPFileTransfer()).thenReturn(mockSCPFileTransfer);
  SFTPClient mockSFTP = mock(SFTPClient.class);
  when(mMockSSHClient.newSFTPClient()).thenReturn(mockSFTP);

  Path from = FileSystems.getDefault().getPath(".");
  String to = "foo";

  mSSHEnv.copyFilesFromLocalDisk(from, to);

  verify(mMockSSHClient).newSCPFileTransfer();
  String expectedDestination = FilenameUtils.concat(REMOTE_HOME_PATH, to);
  verify(mockSCPFileTransfer).upload(any(LocalSourceFile.class), eq(expectedDestination));
}
 
开发者ID:gregorias,项目名称:dfuntest,代码行数:17,代码来源:SSHEnvironmentTest.java

示例6: getTempLocalInstance

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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;
}
 
开发者ID:prateek,项目名称:ssh-spool-source,代码行数:29,代码来源:SshClientJ.java

示例7: upload

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Override
public void upload(final File source, final String destination) throws IOException {

    final char separator = platform.getSeparator();
    final String destination_path;
    if (destination.endsWith(String.valueOf(separator))) {
        destination_path = SimplePlatform.addTailingSeparator(separator, destination);
    }
    else {
        destination_path = destination;
    }
    final SFTPClient sftp = ssh.newSFTPClient();
    LOGGER.debug("Uploading {} to {} on host {} ", source, destination, getName());
    upload(sftp, source, destination_path);
}
 
开发者ID:stacs-srg,项目名称:shabdiz,代码行数:16,代码来源:SSHHost.java

示例8: download

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Override
public void download(final String source, final File destination) throws IOException {

    final SFTPClient sftp = ssh.newSFTPClient();
    LOGGER.debug("downloading {} from host {} to {}", source, getName(), destination);
    sftp.get(source, new FileSystemFile(destination));
}
 
开发者ID:stacs-srg,项目名称:shabdiz,代码行数:8,代码来源:SSHHost.java

示例9: listChildrenNamesByFilter

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
private List<String> listChildrenNamesByFilter(String remotePath, RemoteResourceFilter remoteFolderResourceFilter) throws IOException {
    try (SFTPClient sftpClient = sshClient.newSFTPClient()) {
        List<String> children = new ArrayList<>();
        List<RemoteResourceInfo> childrenInfos = sftpClient.ls(remotePath, remoteFolderResourceFilter);
        childrenInfos.stream().forEach((childInfo) -> {
            children.add(childInfo.getName());
        });
        return children;
    }
}
 
开发者ID:sparsick,项目名称:comparison-java-ssh-libs,代码行数:11,代码来源:SshJClient.java

示例10: listFiles

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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"));
}
 
开发者ID:mlk,项目名称:AssortmentOfJUnitRules,代码行数:28,代码来源:TestSftpRule.java

示例11: listFilesUsingNativeSystem

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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;
}
 
开发者ID:fbaligand,项目名称:lognavigator,代码行数:45,代码来源:SshLogAccessService.java

示例12: mkdirs

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的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();
  }
}
 
开发者ID:gregorias,项目名称:dfuntest,代码行数:39,代码来源:SSHEnvironment.java

示例13: copyFilesShouldThrowExceptionWhenSCPFails

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Test(expected = IOException.class)
public void copyFilesShouldThrowExceptionWhenSCPFails() throws IOException {
  SCPFileTransfer mockSCPFileTransfer = mock(SCPFileTransfer.class);
  when(mMockSSHClient.newSCPFileTransfer()).thenReturn(mockSCPFileTransfer);
  SFTPClient mockSFTP = mock(SFTPClient.class);
  when(mMockSSHClient.newSFTPClient()).thenReturn(mockSFTP);
  doThrow(new IOException()).when(mockSCPFileTransfer).upload(
      any(LocalSourceFile.class), anyString());

  Path from = FileSystems.getDefault().getPath(".");
  String to = "foo";

  mSSHEnv.copyFilesFromLocalDisk(from, to);
}
 
开发者ID:gregorias,项目名称:dfuntest,代码行数:15,代码来源:SSHEnvironmentTest.java

示例14: copyFilesShouldThrowExceptionWhenDestinationIsIncorrect

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Test(expected = IllegalArgumentException.class)
public void copyFilesShouldThrowExceptionWhenDestinationIsIncorrect() throws IOException {
  SCPFileTransfer mockSCPFileTransfer = mock(SCPFileTransfer.class);
  when(mMockSSHClient.newSCPFileTransfer()).thenReturn(mockSCPFileTransfer);
  SFTPClient mockSFTP = mock(SFTPClient.class);
  when(mMockSSHClient.newSFTPClient()).thenReturn(mockSFTP);

  Path from = FileSystems.getDefault().getPath(".");
  String to = "../../..";

  mSSHEnv.copyFilesFromLocalDisk(from, to);
}
 
开发者ID:gregorias,项目名称:dfuntest,代码行数:13,代码来源:SSHEnvironmentTest.java

示例15: mkdirsShouldUseSFTPFirstAndDestinationPathShouldContainDot

import net.schmizz.sshj.sftp.SFTPClient; //导入依赖的package包/类
@Test
public void mkdirsShouldUseSFTPFirstAndDestinationPathShouldContainDot() throws IOException {
  SFTPClient mockSFTP = mock(SFTPClient.class);
  when(mMockSSHClient.newSFTPClient()).thenReturn(mockSFTP);
  mSSHEnv.mkdirs("mock");

  String properDestinationPath = "./" + FilenameUtils.concat(REMOTE_HOME_PATH, "mock");
  verify(mockSFTP).mkdirs(eq(properDestinationPath));
}
 
开发者ID:gregorias,项目名称:dfuntest,代码行数:10,代码来源:SSHEnvironmentTest.java


注:本文中的net.schmizz.sshj.sftp.SFTPClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。