Created
November 21, 2013 10:24
-
-
Save Arakaki/7579297 to your computer and use it in GitHub Desktop.
ScalaでSFTP経由でファイル取得(LoanPatternバージョン)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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