SFTPサーバからリモートサーバにファイルをダウンロードするJavaプログラムを開発したい。そのリモートサーバーには共有パスがありません。私はsftpから直接ダウンロードして、リモートのWindowsサーバードライバ(D :)に貼り付ける必要があります。SFTPファイルをリモートサーバフォルダに直接ダウンロードする(共有パスではない)
コード:
int ftpPort = 22;
JSch jsch = new JSch ();
Session session = null;
Channel channel = null;
ChannelSftp c = null;
try {
session = jsch.getSession(username, hostname, ftpPort);
logger.info("*** FTP Session created. ***");
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
logger.info("*** Session connected. ***");
//Open the SFTP channel
logger.info("*** Opening FTP Channel. ***");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
//Change to the remote directory
logger.info("*** Changing to FTP remote dir: " + remoteDirectory + " ***");
c.cd(remoteDirectory);
//Send the file we generated
try {
logger.info("*** Storing file:'" + filename + "' to local directory: '"+localDirectory+"'");
私はSFTPサーバに接続するためのJSCHとChannelSftpを使用しています。
現在、上記のコードは、コードをローカルパスと共有パスにダウンロードします。
共有パスを持たないリモートサーバー(Windows)にファイルをダウンロードするよう提案されています。
ありがとうございました。