我有一个问题,希望得到一个解决方案。 我也编写了一些代码,但需要进行一些修改。
问题:我需要连接的SFTP服务器(出于隐私目的,我将提供虚拟凭据)。
服务器名称:服务器名称端口:22用户名:用户名密码:密码
当我连接到服务器时,它会自动将我放入“/ FGV”目录中。 在这个目录里面是其他几个文件夹。 我需要从“/ FGV / US / BS /”目录中获取xml消息列表,并将它们放在LIST(文件形式的文件)中。 在列表中,我需要有文件目录,文件名和文件正文。 我正在考虑创建一个对象并将此信息放在那里并创建该对象的List。
我当前的代码创建一个连接并仅下载一个xml文件。 如果有两个xml文件,那么本地计算机中的文件没有任何内容。
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 SFTPinJava {
public SFTPinJava() {
}
public static void main(String[] args) {
String SFTPHOST = "server-name";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "/FGV";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
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("/FGV/US/BS/FGVCustomsEntryLoaderService.xml"));
File newFile = new File(
"C:\\workspace\\Crap\\src\\org\\raghav\\stuff\\XML_FROM_SERVER.xml");
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
//System.out.println("Getting: " + theLine);
while ((readCount = bis.read(buffer)) > 0) {
//System.out.println("Writing: ");
bos.write(buffer, 0, readCount);
}
while(session != null){
System.out.println("Killing the session");
session.disconnect();
bis.close();
bos.close();
System.exit(0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我需要更改此代码,以便它可以获取多个文件并将它们放在对象列表中。 该对象应具有文件目录,文件名和文件正文。