sftp + java,获取ftp文件,用的freesshd

package com.qing.qing.test;

import com.jcraft.jsch.*;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.apache.commons.io.FileUtils;
import sun.awt.image.ByteInterleavedRaster;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

public class Test2 {
public static void main(String[] args) {
Session session = null;
Channel channel = null;
try {
JSch jSch = new JSch();
session = jSch.getSession("admin", "192.168.0.180",22);
session.setPassword("666666");
Properties sshConfig = new Properties();
// ssh登录时不需要点击确定等验证
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
setUser(session);

channel = session.openChannel("sftp");
channel.connect(3000);
ChannelSftp sftp = (ChannelSftp) channel; //强制转换

// channel = session.openChannel("shell");
// channel.connect(3000);
// ChannelShell sftp = (ChannelShell) channel; //强制转换

//取得文件列表
Vector vector = sftp.ls("*");
Iterator iterator = vector.iterator();
int index = 0;
while (iterator.hasNext()) {
index++;
ChannelSftp.LsEntry obj = (ChannelSftp.LsEntry) iterator.next();
//取得文件名
String file = obj.getFilename();
//根据文件名获取文件流
InputStream inputStream = sftp.get(file);

byte[] buffer = TestBM.getByteArray(inputStream);
String charStr = TestBM.getFilecharset(new ByteArrayInputStream(buffer));

System.out.println("编码格式="+charStr);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buffer), charStr));
String ss = null;

while((ss = bufferedReader.readLine()) != null){
System.out.println(" " + ss);
}

bufferedReader.close();

}
// channel = session.openChannel("exec");
// ChannelExec sftp = (ChannelExec) channel; //强制转换
// sftp.setCommand("ls");
// channel.setInputStream(null);

} catch (Exception e) {
e.printStackTrace();
}finally {
if(channel != null) channel.disconnect();
if(session != null) session.disconnect();
}
}

public static void setUser(Session session){
UserInfo ui = new MyUserInfo(){
public void showMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
public boolean promptYesNo(String message){
Object[] options={ "yes", "no" };
int foo=JOptionPane.showOptionDialog(null,
message,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
return foo==0;
}

// If password is not given before the invocation of Session#connect(),
// implement also following methods,
// * UserInfo#getPassword(),
// * UserInfo#promptPassword(String message) and
// * UIKeyboardInteractive#promptKeyboardInteractive()

};

session.setUserInfo(ui);
}

public static abstract class MyUserInfo
implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return null; }
public boolean promptYesNo(String str){ return false; }
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return false; }
public boolean promptPassword(String message){ return false; }
public void showMessage(String message){ }
public String[] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo){
return null;
}
}
}
原文地址:https://www.cnblogs.com/shihx/p/13500096.html