LoginSignup
11
18

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-02-07

はじめに

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

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

put.txt(UTF-8)
テストファイルです

実装例

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

SFTPTest.java
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.nio.charset.StandardCharsets;

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

    public static void main(String[] args) throws JSchException, SftpException, 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(), StandardCharsets.UTF_8));
        }
        finally {
            if (fin != null) {
                try {
                    fin.close();
                }
                catch (IOException e) {
                }
            }
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (IOException e) {
                }
            }
            if (channel != null) {
                try {
                    channel.disconnect();
                }
                catch (Exception e) {
                }
            }
            if (session != null) {
                try {
                    session.disconnect();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

動作確認

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

環境

  • 開発

    • Windows 10 Pro
    • JDK 1.8.0_112
    • NetBeans IDE 8.2
  • 動作検証

    • CentOS Linux release 7.2
    • JDK 1.8.0_112

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

11
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
18