Skip to content

Instantly share code, notes, and snippets.

@Arakaki
Created November 21, 2013 10:24
Show Gist options
  • Save Arakaki/7579297 to your computer and use it in GitHub Desktop.
Save Arakaki/7579297 to your computer and use it in GitHub Desktop.
ScalaでSFTP経由でファイル取得(LoanPatternバージョン)
import com.jcraft.jsch.{Session, ChannelSftp, JSch}
object App {
def using[A,B]( resource:A )( func: A => B )( endAction: A => Unit ){
try {
func(resource)
}catch{
case e:Exception => e.printStackTrace()
} finally {
endAction(resource)
}
}
def main(args: Array[String]){
val jsch = new JSch
val userName = "Gakkie"
val hostName = "localhost"
val password = "password"
using(jsch.getSession(userName,hostName,22)){ s =>
s.setPassword(password)
s.connect()
using(s.openChannel("sftp").asInstanceOf[ChannelSftp]){ c =>
c.connect()
//sftpでファイルを取得
c.get("/tmp/src.csv","/tmp/dist.csv")
}{c => c.disconnect()}
}{s => s.disconnect()}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment