Skip to content

Instantly share code, notes, and snippets.

@Arakaki
Last active December 28, 2015 23:29
Show Gist options
  • Save Arakaki/7579002 to your computer and use it in GitHub Desktop.
Save Arakaki/7579002 to your computer and use it in GitHub Desktop.
ScalaでSFTP経由でファイル取得(Loan Patternバージョン)
import com.jcraft.jsch.{Session, ChannelSftp, JSch}
object App {
def using(s: Session)(f: Session => Any){
try {
f(s)
}catch{
case e:Exception => e.printStackTrace
} finally {
s.disconnect()
}
}
def using(c: ChannelSftp)(f: ChannelSftp => Any){
try {
f(c)
}catch{
case e:Exception => e.printStackTrace
} finally {
c.disconnect()
}
}
def main(args: Array[String]){
val userName = "Gakkie"
val hostName = "localhost"
val password = "hogehoge"
val jsch = new JSch
using(jsch.getSession(userName,hostName,22)){ s =>
s.setPassword(password)
using(s.openChannel("sftp").asInstanceOf[ChannelSftp]){ c =>
c.connect()
//sftpでファイルを取得
c.get("/tmp/ahoge_src.tsv","/tmp/ahoge_dist.tsv")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment