Last active
December 28, 2015 23:29
-
-
Save Arakaki/7579002 to your computer and use it in GitHub Desktop.
ScalaでSFTP経由でファイル取得(Loan Patternバージョン)
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(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