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


Java ChannelSftp.put方法代码示例

本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.put方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.put方法的具体用法?Java ChannelSftp.put怎么用?Java ChannelSftp.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.jcraft.jsch.ChannelSftp的用法示例。


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

示例1: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void upload(String filename) {
    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(login, server, port);
        session.setPassword(password);

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp channelSftp = (ChannelSftp) channel;
        channelSftp.cd(workingDirectory);
        File f = new File(filename);

        channelSftp.put(new FileInputStream(f), f.getName());

        f.delete();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:SKNZ,项目名称:LesPatternsDuSwag,代码行数:26,代码来源:SFTPUploader.java

示例2: put

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Send a file to server path via SFTP
 * @param src
 * @param path
 * @throws SftpException
 */
public void put(final File src, String path) throws SftpException
{
    if (!path.endsWith("/")) path = path.concat("/");
    if (path.startsWith("/")) path = path.substring(1);
    
    ChannelSftp sftp = (ChannelSftp) channel;
    SftpProgressMonitor progress = new SftpProgressMonitor() {
        
        @Override public void init(int arg0, String arg1, String arg2, long arg3)
        {
            System.out.println("File transfer begin..");
        }
        
        @Override public void end()
        {
            Out.print(LOG_LEVEL.INFO, "Upload of file "+ src.getName() +" succeeded.");
            ready = true;
        }
        
        @Override public boolean count(long i) { return false; }
    };
    sftp.put(src.getAbsolutePath(), initRemDir+path+src.getName(), progress);
}
 
开发者ID:DevComPack,项目名称:setupmaker,代码行数:30,代码来源:JschFactory.java

示例3: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Creates a new file on the remote host using the input content.
 *
 * @param from the byte array content to be uploaded
 * @param fileName the name of the file for which the content will be saved into
 * @param toPath the path of the file for which the content will be saved into
 * @param isUserHomeBased true if the path of the file is relative to the user's home directory
 * @param filePerm file permissions to be set
 * @throws Exception exception thrown
 */
public void upload(InputStream from, String fileName, String toPath, boolean isUserHomeBased, String filePerm) throws Exception {
    ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp");
    channel.connect();
    String absolutePath = isUserHomeBased ? channel.getHome() + "/" + toPath : toPath;

    String path = "";
    for (String dir : absolutePath.split("/")) {
        path = path + "/" + dir;
        try {
            channel.mkdir(path);
        } catch (Exception ee) {
        }
    }
    channel.cd(absolutePath);
    channel.put(from, fileName);
    if (filePerm != null) {
        channel.chmod(Integer.parseInt(filePerm), absolutePath + "/" + fileName);
    }

    channel.disconnect();
}
 
开发者ID:Azure-Samples,项目名称:acr-java-manage-azure-container-registry,代码行数:32,代码来源:SSHShell.java

示例4: copyFile

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void copyFile(String fromFilePath, String toFilePath, boolean closeSession) {
    ChannelSftp uploadSftp = null;
    InputStream inputStream = null;
    try {
        // Get a reusable channel if the session is not auto closed.
        uploadSftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
        uploadSftp.cd(basePath);
        inputStream = getInputStream(fromFilePath, true);
        uploadSftp.put(inputStream, toFilePath);
    } catch (Exception e) {
        throw new IoException("Error copying file.  Error %s", e.getMessage());
    } finally {
        if (closeSession) {
            close();
        }
    }  
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:19,代码来源:SftpDirectory.java

示例5: copyToDir

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void copyToDir(String fromFilePath, String toDirPath, boolean closeSession) {
    ChannelSftp uploadSftp = null;
    FileInfo fileInfo = new FileInfo(fromFilePath, false, new java.util.Date().getTime(), -1);
    InputStream inputStream = null;
    try {
        // Get a reusable channel if the session is not auto closed.
        uploadSftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
        uploadSftp.cd(basePath);
        
        if (!toDirPath.endsWith("/")) {
        	toDirPath += "/";
        }
        inputStream = getInputStream(fromFilePath, true);
        uploadSftp.put(inputStream, toDirPath + fileInfo.getName());
    } catch (Exception e) {
        throw new IoException("Error copying directory.  Error %s", e.getMessage());
    } finally {
        if (closeSession) {
            close();
        }
    }
}
 
开发者ID:JumpMind,项目名称:metl,代码行数:24,代码来源:SftpDirectory.java

示例6: put

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * 本地檔案放到遠端SFTP上
 * 	
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param localFile
 * @param remoteFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void put(String user, String password, String addr, int port,
		List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (int i=0; i<localFile.size(); i++) {
			String rf=remoteFile.get(i);
			String lf=localFile.get(i);
			logger.info("put local file: " + lf + " write to " + addr + " :" + rf );
			sftpChannel.put(lf, rf);
			logger.info("success write to " + addr + " :" + rf);
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}		
}
 
开发者ID:billchen198318,项目名称:bamboobsc,代码行数:38,代码来源:SFtpClientUtils.java

示例7: copyFile

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
	 * 
	 * @param sourceFilePath
	 * @param destinationFilePath
	 * @param destinationHost
	 * @param destinationPort
	 * @param destinationUserName
	 * @param destinationUserPassword
	 * @param destinationPrivateKeyFilePath
	 * @param destinationPrivateKeyPassphrase
	 * @throws IOException
	 * @throws JSchException
	 * @throws SftpException
	 */
	public static void copyFile(
			String sourceFilePath,
			String destinationFilePath,
			String destinationHost,
			String destinationPort,
			String destinationUserName,
			String destinationUserPassword,
			String destinationPrivateKeyFilePath,
			String destinationPrivateKeyPassphrase) throws IOException, JSchException, SftpException {
		
		JSch jsch = new JSch();
		
		if (!StringUtils.isEmptyOrNull(destinationPrivateKeyFilePath)) {
			jsch.addIdentity(destinationPrivateKeyFilePath, destinationPrivateKeyPassphrase);
		}
		
		int destinationPortNumber = 22;
		if (!StringUtils.isEmptyOrNull(destinationPort)) {
			destinationPortNumber = Integer.parseInt(destinationPort);
		}
		
		Session session = jsch.getSession(destinationUserName, destinationHost, destinationPortNumber);
		if (!StringUtils.isEmptyOrNull(destinationUserPassword)) {
			session.setPassword(destinationUserPassword);
		}
		
//		Properties config = new Properties();
//        config.put("StrictHostKeyChecking", "no");
//        session.setConfig(config);
        session.connect();
        
        Channel channel = session.openChannel("sftp");
        channel.connect();
        
        ChannelSftp channelSftp = (ChannelSftp)channel;        
        File destinationFile = new File(destinationFilePath);
        channelSftp.cd(destinationFile.getPath());
        channelSftp.put(new FileInputStream(sourceFilePath), destinationFile.getName());
		
		
	}
 
开发者ID:Web-of-Building-Data,项目名称:Ifc2Rdf,代码行数:56,代码来源:SshFileUploader.java

示例8: transferFile

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Transfer a file to remote destination via JSCH library using sFTP protocol
 * 
 * @param username String remote SFTP server user name.
 * @param password String remote SFTP server user password
 * @param host String remote SFTP server IP address or host name.
 * @param file File to transfer to SFTP Server.
 * @param transferProtocol protocol to transfer a file. {@link FileTransferProtocol}
 * @return boolean true if file is transfered otherwise false.
 * @throws ApplicationException
 */
public static boolean transferFile(final String username, final String password, final String host,
                                   final File file, final FileTransferProtocol transferProtocol)
    throws ApplicationException {
    // currently can deal with sftp only.
    LOGGER.trace("Invoking transferFile...");
    JSch jsch = new JSch();
    try {
        Session session = jsch.getSession(username, host);
        LOGGER.debug("Session Host: " + session.getHost());
        session.setPassword(password);
        Properties properties = new Properties();
        properties.put("StrictHostKeyChecking", "no");
        session.setConfig(properties);
        LOGGER.debug("Connecting to a session Host Server...");
        session.connect();
        LOGGER.debug("session is established with host server.");
        Channel channel = session.openChannel(transferProtocol.ftpStringRepresentation());
        LOGGER.debug("Connecting to a sftp Channel...");
        channel.connect();
        LOGGER.debug("Connected with sftp Channel.");
        ChannelSftp channelSftp = (ChannelSftp) channel;
        channelSftp.put(new FileInputStream(file), file.getName());
        LOGGER.debug("File transfered successfully");
        channelSftp.exit();
        LOGGER.debug("sftp channel disconnected.");
        channel.disconnect();
        LOGGER.debug("channel disconnected.");
        session.disconnect();
        LOGGER.debug("session disconnected.");
        return true;
    } catch (JSchException | FileNotFoundException | SftpException e) {
        LOGGER.error(e.getMessage(), e.getCause());
        throw new ApplicationException(e.getMessage(), ApplicationSeverity.ERROR, e.getCause(), e);
    }
}
 
开发者ID:SanjayMadnani,项目名称:com.sanjay.common.common-utils,代码行数:47,代码来源:FileUtil.java

示例9: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void upload(LocalResource resource, URI destination) throws IOException {
    LockableSftpClient client = sftpClientFactory.createSftpClient(destination, credentials);

    try {
        ChannelSftp channel = client.getSftpClient();
        ensureParentDirectoryExists(channel, destination);
        InputStream sourceStream = resource.open();
        try {
            channel.put(sourceStream, destination.getPath());
        } finally {
            sourceStream.close();
        }
    } catch (com.jcraft.jsch.SftpException e) {
        throw ResourceExceptions.putFailed(destination, e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:20,代码来源:SftpResourceUploader.java

示例10: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
 * Perform the specified SSH file upload.
 * 
 * @param from source local file URI ({@code file} protocol)
 * @param to target remote folder URI ({@code ssh} protocol)
 */
private static void upload(URI from, URI to) {
    try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, to);
            FileInputStream fis = new FileInputStream(new File(from))) {

        LOG.info("Uploading {} --> {}", from, session.getMaskedUri());
        ChannelSftp channel = session.getChannel();
        channel.connect();
        channel.cd(to.getPath());
        channel.put(fis, getName(from.getPath()));

    } catch (Exception e) {
        throw new RemoteFileUploadFailedException("Cannot upload file", e);
    }
}
 
开发者ID:Nordstrom,项目名称:Remote-Session,代码行数:21,代码来源:SshUtils.java

示例11: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void upload(String directory, String uploadFile, ChannelSftp sftp) throws Exception{
	
	File file = new File(uploadFile);
	if(file.exists()){
		
		try {
			Vector content = sftp.ls(directory);
			if(content == null){
				sftp.mkdir(directory);
				System.out.println("mkdir:" + directory);
			}
		} catch (SftpException e) {
			sftp.mkdir(directory);
		}
		sftp.cd(directory);
		System.out.println("directory: " + directory);
		if(file.isFile()){
			InputStream ins = new FileInputStream(file);
			
			sftp.put(ins, new String(file.getName().getBytes(),"UTF-8"));
			
		}else{
			File[] files = file.listFiles();
			for (File file2 : files) {
				String dir = file2.getAbsolutePath();
				if(file2.isDirectory()){
					String str = dir.substring(dir.lastIndexOf(file2.separator));
					directory = directory + str;
				}
				System.out.println("directory is :" + directory);
				upload(directory,dir,sftp);
			}
		}
	}
}
 
开发者ID:zhuyuqing,项目名称:bestconf,代码行数:36,代码来源:SFTPUtil.java

示例12: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void upload(File srcFile, String destFile, long timeout) throws JSchException, SftpException, IOException {
    Channel channel = this.session.openChannel("sftp");
    channel.setInputStream(System.in);
    channel.setOutputStream(System.out);
    channel.connect();
    channels.add(channel);

    new ChanneOperationTimer(channel, timeout).start();
    ChannelSftp sftp = (ChannelSftp) channel;
    sftp.put(FileUtils.openInputStream(srcFile), destFile);
    sftp.exit();
}
 
开发者ID:tascape,项目名称:reactor,代码行数:13,代码来源:SshCommunication.java

示例13: put

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void put(ChannelSftp channel, File file) throws SftpException, FileNotFoundException {
InputStream is = null;
try {
	is = new BufferedInputStream(new FileInputStream(file));
	channel.put(is, file.getName());
} finally {
	if (is != null) {
		try {
               is.close();
              } catch (IOException e) {
              	logger.error("Unable to close input stream for " + file, e);
              }
	}
}

  }
 
开发者ID:williamgrosset,项目名称:OSCAR-ConCert,代码行数:17,代码来源:SshHandler.java

示例14: uploadFile

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void uploadFile(String hostname, int port, String username, File keyFile, final String passphrase,
		File file, String destinationFolder) throws JSchException, IOException, SftpException {
	Session session = createSession(hostname, port, username, keyFile, passphrase);
	session.connect();
	ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
	channel.connect();
	channel.cd(destinationFolder);
	channel.put(new FileInputStream(file), file.getName());
	channel.disconnect();
	session.disconnect();
}
 
开发者ID:ujmp,项目名称:universal-java-matrix-package,代码行数:12,代码来源:JSchUtil.java

示例15: upload

import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void upload(Factory<InputStream> sourceFactory, Long contentLength, URI destination) throws IOException {
    LockableSftpClient client = sftpClientFactory.createSftpClient(destination, credentials);

    try {
        ChannelSftp channel = client.getSftpClient();
        ensureParentDirectoryExists(channel, destination);
        InputStream sourceStream = sourceFactory.create();
        try {
            channel.put(sourceStream, destination.getPath());
        } finally {
            sourceStream.close();
        }
    } catch (com.jcraft.jsch.SftpException e) {
        throw new SftpException(String.format("Could not write to resource '%s'.", destination), e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
 
开发者ID:Pushjet,项目名称:Pushjet-Android,代码行数:19,代码来源:SftpResourceUploader.java


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