Sono nuovo di java e sto provando a scrivere uno script che estrarrà più file da vari siti SFTP ogni giorno.Download di più file tramite SFTP utilizzando Java
Ho il codice qui sotto che tirerà 1 file da 1 sito e funziona, tuttavia non riesco a trovare come modificare il mio codice in modo che possa scaricare più file. Quindi, ad esempio, tutti i file nella directory remota o solo determinati file contenenti determinate lettere
Potete consigliarmi su questo per favore?
Codice: -
package package1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SFTPpullsshkeys {
public SFTPpullsshkeys() {
}
public static void main(String[] args) {
String SFTPHOST = "IP";
int SFTPPORT = 22;
String SFTPUSER = "Username";
String passphrase = "passphrase";
String SFTPWORKINGDIR = "remote directory";
String prikeyfile = "C:\\Open SSH Key.ppk";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
jsch.addIdentity(prikeyfile, passphrase);
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(channelSftp.get("file.csv"));
File newFile = new File("C:\\file.csv");
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while
((readCount = bis.read(buffer)) > 0) {
System.out.println("Writing files to disk: ");
bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Spiegare come questo codice risolve il problema. – ChrisF