Groovy and JSch : SFTP transferring files
Join the DZone community and get the full member experience.
Join For FreeThis is a simple example of how to transfer a file using Groovy, JSch, and SFTP.
@Grab(group='com.jcraft', module='jsch', version='0.1.46')
import com.jcraft.jsch.*
java.util.Properties config = new java.util.Properties()
config.put "StrictHostKeyChecking", "no"
JSch ssh = new JSch()
Session sess = ssh.getSession "user", "server.domain.com", 22
sess.with {
setConfig config
setPassword "somecomplicatedpassword"
connect()
Channel chan = openChannel "sftp"
chan.connect()
ChannelSftp sftp = (ChannelSftp) chan;
def sessionsFile = new File('c:/important_document.doc')
sessionsFile.withInputStream { istream -> sftp.put(istream, "/home/brippe/Documents/important_document.doc") }
chan.disconnect()
disconnect()
}
Opinions expressed by DZone contributors are their own.
Comments