本文整理匯總了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();
}
}
示例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);
}
示例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();
}
示例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();
}
}
}
示例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();
}
}
}
示例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();
}
}
示例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());
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}
}
示例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();
}
示例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);
}
}
}
}
示例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();
}
示例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);
}
}