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


Java SftpClient类代码示例

本文整理汇总了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();
        }
    }
}
 
开发者ID:ibuildthecloud,项目名称:dstack,代码行数:19,代码来源:SshAgentConnectionFactory.java

示例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();
        }
    }
}
 
开发者ID:cloudnautique,项目名称:cloud-cattle,代码行数:19,代码来源:SshAgentConnectionFactory.java

示例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);
}
 
开发者ID:northlander,项目名称:activemft,代码行数:28,代码来源:SftpReceiver.java

示例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");
	}
}
 
开发者ID:northlander,项目名称:activemft,代码行数:40,代码来源:SftpSender.java


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