LoginSignup
0
0

More than 3 years have passed since last update.

【JSch】リモートFTPサーバー内でファイルをコピーするサンプル。

Last updated at Posted at 2020-04-09

stackoverflowの下記の記事を参考にさせていただきました。
https://stackoverflow.com/questions/19869446/copy-a-file-on-remote-server-from-one-directory-to-other-using-jsch/25686815

JSchSample.java
package hello;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpProgressMonitor;

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo() {
        return (args) -> {
            log.info("JSch Start");

        };
    }


    public void cp (String source, String target) throws Exception {
        log.info("COMMAND: cp " + source + " " + target);

        String host = "172.20.10.2";
        int port = 22;
        String user = "user";
        String password = "パスワード";

        JSch jsch;
        Session session = null;
        Channel upChannel = null;
        Channel downChannel = null;
        ChannelSftp uploadChannelSftp = null;
        ChannelSftp downloadChannelSftp = null;
        try {
            jsch = new JSch();
            session = jsch.getSession(user, host, port);
            //known_hostsのチェックをスキップ
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();        

            upChannel = session.openChannel("sftp");
            downChannel = session.openChannel("sftp");
            upChannel.connect();
            downChannel.connect();
            uploadChannelSftp = (ChannelSftp) upChannel;
            downloadChannelSftp = (ChannelSftp) downChannel;
            SftpProgressMonitor monitor = null; //???
            InputStream inputStream = uploadChannelSftp.get(source);

            downloadChannelSftp.put(inputStream, target, monitor);
        } catch (JSchException e) {
            log.error("Auth failure", e);
            throw new Exception(e);
        } finally {
            if (upChannel == null || downChannel == null) {
                System.out.println("Channel is null ...");
            }else if (uploadChannelSftp != null && !uploadChannelSftp.isClosed()){
                uploadChannelSftp.exit();
                downloadChannelSftp.exit();
                uploadChannelSftp.disconnect();
                downloadChannelSftp.disconnect();
            }else if (!upChannel.isClosed()) {
                upChannel.disconnect();
                downChannel.disconnect();
            }
            session.disconnect();
        }
    }
}
0
0
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
0
0