Over a million developers have joined DZone.

Groovy and JSch : SFTP transferring files

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

This 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()
}

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
groovy

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}