【Android】 SFTPによるファイルアップロード
Androidにてファイルのアップロードが必要になった。
【環境】
・Xoom
・Android4.0
jschのライブラリーを使用する。
http://www.jcraft.com/jsch/
jsch.jarをライブラリーにおいて、
コードをガリガリかく。
http://d.hatena.ne.jp/n_shuyo/20060820/1156054408
この辺を参考にさせてもらって。
AsynTask内で処理を行う。
try {
JSch jsch = new JSch();
Hashtable config = new Hashtable();
config.put("StrictHostKeyChecking", "no");
jsch.setConfig(config);
String host="my-server.ne.jp";
String user="user";
int port=22;
Session session=jsch.getSession(user, host, port);
session.setPassword("password");
session.connect();
// sftp remotely
ChannelSftp channel=(ChannelSftp)session.openChannel("sftp");
channel.connect();
String remoteFilePath;
remoteFilePath = remote;
String localFilePath;
localFilePath = params[0];
channel.mkdir(dir);
channel.put(localFilePath, remoteFilePath);
Log.i("localFilePath",localFilePath);
Log.i("remoteFilePath",remoteFilePath);
}catch (Exception e){
Log.e("ERROR", e.toString());
}
Jschに関しては、Exampleを見て対応するのが早そう。
あんまりドキュメントがないねえ・・・。