Dashboard
>
Public Content
>
...
Home
>
JavaStuff
>
SCP a file in Java using JSch
Public Content
Log In
SCP a file in Java using JSch
Browse Space
View
I
nfo
Added by
James Richardson
, last edited by
James Richardson
on Mar 18, 2008
Labels:
(None)
Add Labels
Enter labels to add to this page:
Tip:
Looking for a label? Just start typing.
private static void sftpFile(File localFile, String username, String password, String hostname, String remoteDirectory) throws JSchException, SftpException, FileNotFoundException { JSch jsch = new JSch(); String filename = localFile.getName(); Session session = jsch.getSession(username, hostname, 22); session.setUserInfo(new HardcodedUserInfo(password)); Properties config = new Properties(); config.setProperty("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); ChannelSftp channel = (ChannelSftp)session.openChannel("sftp"); channel.connect(); channel.cd(remoteDirectory); channel.put(new FileInputStream(localFile), filename); channel.disconnect(); session.disconnect(); } private static class HardcodedUserInfo implements UserInfo { private final String password; private HardcodedUserInfo(String password) { this.password = password; } public String getPassphrase() { return null; } public String getPassword() { return password; } public boolean promptPassword(String s) { return true; } public boolean promptPassphrase(String s) { return true; } public boolean promptYesNo(String s) { return true; } public void showMessage(String s) { System.out.println("message = " + s); } }
Add Comment
Powered by
Atlassian Confluence
, the
Enterprise Wiki
. (Version: 2.5.1 Build:#806 May 06, 2007) -
Bug/feature request
-
Contact Administrators