java Mina sftp_java – 使用Apache Mina作为模拟/内存SFTP服务器进行单元测试

我正在解决如何使用Apache Mina的麻烦.他们的文档对我无能为力的大脑来说有一点不足.我已经看到了有用的起始代码

Java SFTP server library?

我无法想像的是如何使用它.我想设置一个检查我的sftp代码的单元测试,使用Mina作为一种模拟服务器,即能够写一个单元测试,如:

@Before

public void beforeTestSetup() {

sshd = SshServer.setUpDefaultServer();

sshd.setPort(22);

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

List> userAuthFactories = new ArrayList>();

userAuthFactories.add(new UserAuthNone.Factory());

sshd.setUserAuthFactories(userAuthFactories);

sshd.setPublickeyAuthenticator(new PublickeyAuthenticator());

sshd.setCommandFactory(new ScpCommandFactory());

List> namedFactoryList = new ArrayList>();

namedFactoryList.add(new SftpSubsystem.Factory());

sshd.setSubsystemFactories(namedFactoryList);

try {

sshd.start();

} catch (Exception e) {

e.printStackTrace();

}

}

@Test

public void testGetFile() {

}

问题是在testGetFile()中放入什么.

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

我相信这是一个很好的API,只是没有很多的指导,任何人都可以帮忙?

这是我做的(JUnit):

@Test

public void testPutAndGetFile() throws JSchException,SftpException,IOException

{

JSch jsch = new JSch();

Hashtable config = new Hashtable();

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,numRead));

}

} finally {

reader.close();

}

return fileData.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值