采用JSCH API(本例引用了jsch-0.1.52.jar)
官网参考 http://www.jcraft.com/jsch/
官网参考 http://www.jcraft.com/jsch/
1,建立Session,对应一个用户账户,并在无传输线程时自动关闭session
public class SFTPProcessor2 {
private static final Logger LOGGER = Logger.getLogger(SFTPProcessor2.class);
private static Session session = null;
public Session getConnect(Map<String, String> serverMap) {
String ftpHost = serverMap.get(SFTPConstants.SFTP_SERVER_HOST);
String port = serverMap.get(SFTPConstants.SFTP_SERVER_PORT);
String ftpUserName = serverMap.get(SFTPConstants.SFTP_SERVER_USERNAME);
String ftpPassword = serverMap.get(SFTPConstants.SFTP_SERVER_PASSWORD);
// 默认的端口22 此处我是定义到常量类中;
int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
// 判断端口号是否为空,如果不为空,则赋值
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 创建JSch对象
// 按照用户名,主机ip,端口获取一个Session对象
try {
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
LOGGER.debug("Session created.");
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 设置密码
}
// 具体config中需要配置那些内容,请参照sshd服务器的配置文件/etc/ssh/sshd_config的配置
Properties config = new Properties();
// 设置不用检查hostKey
// 如果设置成“yes”,ssh就会自动把计算机的密匙加入“$HOME/.ssh/known_hosts”文件,
// 并且一旦计算机的密匙发生了变化,就拒绝连接。
config.put("StrictHostKeyChecking", "no");
// UseDNS指定,sshd的是否应该看远程主机名,检查解析主机名的远程IP地址映射到相同的IP地址。
// 默认值是 “yes” 此处是由于我们SFTP服务器的DNS解析有问题,则把UseDNS设置为“no”
config.put("UseDNS", "no");
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(SFTPConstants.SFTP_DEFAULT_TIMEOUT); // 设置timeout时候
session.connect(); // 经由过程Session建树链接
LOGGER.debug("Session connected.");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return session;
}
/* public boolean upfile(InputStream is, OutputStream os) throws IOException{
boolean res = false;
byte[] b = new byte[1024*1024*100];
int read;
if(os!=null){
do {
read = is.read(b, 0, b.length);
if (read > 0) {
os.write(b, 0, read);
}
os.flush();
} while (read >= 0);
}
return res;
}*/
/* public void uploadFile(String localFile, String newName,
String remoteFoldPath) // throws AppBizException
{
InputStream input = null;
OutputStream os = null;
try {
File lf = new File(localFile);
input = new FileInputStream(lf);
// 改变当前路径到指定路径
channel.cd(remoteFoldPath);
long t1 = System.currentTimeMillis();
//channel.put(input, newName);
os = channel.put(newName
//, new ProgressMonitor(lf.length()) // 上传时不执行init()方法
,new ProgressMo