15

我在弄清楚如何使用 Apache Mina 时遇到了一些麻烦。他们的文档对于我那无能的大脑来说有点不足。我在 Java SFTP 服务器库中看到了有用的起始代码?

我不知道如何使用它。我想设置一个单元测试来检查我的 sftp 代码,使用 Mina 作为一种模拟服务器,即能够编写一个单元测试,例如:

@Before 
public void beforeTestSetup() {
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(22);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
    userAuthFactories.add(new UserAuthNone.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator());


    sshd.setCommandFactory(new ScpCommandFactory());

    List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
    namedFactoryList.add(new SftpSubsystem.Factory());
    sshd.setSubsystemFactories(namedFactoryList);

    try {
        sshd.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void testGetFile() {

}

问题是要放入什么testGetFile()

我一直在浏览测试代码,想知道上面是否需要更多配置来指定根目录、用户名和身份验证密钥文件名。然后我需要使用客户端或我自己的 SFTP api 代码从中获取和提取文件?

我确信这是一个优秀的 API,只是没有太多的说明,有人可以帮忙吗?

4

1 回答 1

9

这是我所做的(JUnit):

  @Test
  public void testPutAndGetFile() throws JSchException, SftpException, IOException
  {
    JSch jsch = new JSch();

    Hashtable<String, String> config = new Hashtable<String, String>();
    config.put("StrictHostKeyChecking", "no");
    JSch.setConfig(config);

    Session session = jsch.getSession( "remote-username", "localhost", PORT);
    session.setPassword("remote-password");

    session.connect();

    Channel channel = session.openChannel( "sftp" );
    channel.connect();

    ChannelSftp sftpChannel = (ChannelSftp) channel;

    final String testFileContents = "some file contents";

    String uploadedFileName = "uploadFile";
    sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);

    String downloadedFileName = "downLoadFile";
    sftpChannel.get(uploadedFileName, downloadedFileName);

    File downloadedFile = new File(downloadedFileName);
    Assert.assertTrue(downloadedFile.exists());

    String fileData = getFileContents(downloadedFile);

    Assert.assertEquals(testFileContents, fileData);

    if (sftpChannel.isConnected()) {
      sftpChannel.exit();
      System.out.println("Disconnected channel");
    }

    if (session.isConnected()) {
      session.disconnect();  
      System.out.println("Disconnected session");
    }

  }

  private String getFileContents(File downloadedFile)
    throws FileNotFoundException, IOException
  {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(new FileReader(downloadedFile));

    try {
      char[] buf = new char[1024];
      for(int numRead = 0; (numRead = reader.read(buf)) != -1; buf = new char[1024]) {
        fileData.append(String.valueOf(buf, 0, numRead));
      }
    } finally {    
      reader.close();
    }

    return fileData.toString();
  }
于 2012-08-13T22:55:46.797 回答