當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。