public static ChannelSftp connect(String username,String password,String hostname,String remoteDir,int port){
//remoteDir远程目录 文件下载的目录
ChannelSftp sftp =null;
Session sshSession=null;
String loaclpacth="d:\\download ";//下载到本来路径
String fileName=" ";//文件名
String zipName=" ";//可以通过参数传递过来
try{
JSch jsch=new Jsch();
sshSession =jsch.getSession(username,hostname,port);
sshSession.setPassword(password);
Properties sshConfig=new Properties();
sshConfig.put("StrictHostKeyChecking","no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channle=sshSession.openChannel("sftp");
channel.connect();
sftp=(Channel)channel;
//下面这段是针对linux系统才用的,window 可以省略一部分
Voctor list = sftp.ls(remoteDir);
String listName="";
File remoteDirFile=null;
for(int i=0;i
listName=list.get(i).toString();
String [] arr=listName.split(" ");
if(arr !=null && arr[arr.length-1].equals(zipName){
flag=true;
break;
}
}
if(flag){
String unzipUrl= remoteDir+"/aaa.csv";//远程下载地址跟文件名
SftpATTRS attr=sftp.stat(unzipUrl);
long filezie=attr.getSize();
String fileName=loaclpacth+"/aaa.csv";//下载到本地路径
OutputStream out = new FileOutputStream(fileName);
sftp.get(unzipUrl,fileName);//下载 unzipUrl远程下载地址 下载到本地fileName;
//在解压zip文件。。。。。
unzip(filename,zip文件路径)
}
}catch(exception e){
}finally{
sftp.quit();
sshSession.disconnect();
}
return sftp;
}