Java SFTPでアップロード・ダウンロード(known_hosts不要)

はじめに

事前に以下のライブラリを用意します。

アップロードする以下のファイルを使います。

テストファイルです

実装例

今回は、アップロードしたファイルをダウンロードして内容確認することとします。 サンプルでは、動作確認しやすいようにmainメソッドで実行できるようにしてあります。

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author tool-taro.com
 */
public class SFTPTest {

    public static void main(String[] args) throws JSchException, SftpException, UnsupportedEncodingException, FileNotFoundException, IOException {

        //サーバ
        String host = "ホスト名";
        //ポート
        int port = 22;
        //ユーザ
        String user = "ユーザ";
        //パスワード
        String password = "パスワード";
        //ディレクトリ
        String dir = "/ディレクトリ";
        //アップロード・ダウンロードするファイル名
        String fileName = "put.txt";

        JSch jsch;
        Session session = null;
        ChannelSftp channel = null;
        FileInputStream fin = null;
        BufferedInputStream bin = null;

        try {
            //接続
            jsch = new JSch();
            session = jsch.getSession(user, host, port);
            //known_hostsのチェックをスキップ
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            //ディレクトリ移動
            channel.cd(dir);

            //アップロード
            fin = new FileInputStream(fileName);
            channel.put(fin, fileName);

            //ダウンロード
            bin = new BufferedInputStream(channel.get(fileName));
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int length;
            while (true) {
                length = bin.read(buf);
                if (length == -1) {
                    break;
                }
                bout.write(buf, 0, length);
            }
            //標準出力
            System.out.format("ダウンロードしたファイル=%1$s", new String(bout.toByteArray(), "UTF-8"));
        }
        finally {
            if (fin != null) {
                try {
                    fin.close();
                }
                catch (Exception e) {
                }
            }
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (Exception e) {
                }
            }
            if (channel != null) {
                try {
                    channel.disconnect();
                }
                catch (Exception e) {
                }
            }
            if (session != null) {
                try {
                    session.disconnect();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

動作確認

$ javac SFTPTest.java
$ java SFTPTest
$ 取得したファイル=テストファイルです

環境

Webツールも公開しています。 Web便利ツール@ツールタロウ