java SFTP

土豆你个番茄 2013-05-22 01:55:53
用jsch包

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SFTPChannel {
Session session = null;
Channel channel = null;

private static final Logger LOG = Logger.getLogger(SFTPChannel.class
.getName());

public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout)
throws JSchException {

String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);

int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}

JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
LOG.debug("Session created.");
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 设置密码
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
LOG.debug("Session connected.");

LOG.debug("Opening Channel.");
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect(); // 建立SFTP通道的连接
LOG.debug("Connected successfully to ftpHost = " + ftpHost
+ ",as ftpUserName = " + ftpUserName + ", returning: "
+ channel);
return (ChannelSftp) channel;
}

public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}

public static void main(String[] args) {
try {
SFTPChannel sftpChannel = new SFTPChannel();

Map<String, String> sftpDetails = new HashMap<String, String>();
// 设置主机ip,端口,用户名,密码
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "192.168.0.197");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "zhengjf");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "lmis123");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22");

String src = "C:\\lmis\\AutoRelease\\data\\tempprogram\\gethash.sh"; // 本地文件名
String dst = "/zhengjf/test/test.sh"; // 目标文件名

ChannelSftp chSftp = sftpChannel.getChannel(sftpDetails, 60000);

chSftp.put(src, dst, ChannelSftp.OVERWRITE);


chSftp.quit();

sftpChannel.closeChannel();
} catch (Exception e) {
e.printStackTrace();
}

}

}




报的异常
log4j:WARN No appenders could be found for logger (com.longyg.sftp.SFTPChannel).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:491)
at com.jcraft.jsch.Session.connect(Session.java:162)
at com.longyg.sftp.SFTPChannel.getChannel(SFTPChannel.java:45)
at com.longyg.sftp.SFTPChannel.main(SFTPChannel.java:80)




...全文
201 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
土豆你个番茄 2013-08-12
  • 打赏
  • 举报
回复
引用 3 楼 zyqin 的回复:
请问楼主,“乱码问题”具体指的是什么问题?我也遇到这个问题,可以登录linux,但是登录solaris的时候报错“Auth fail“。谢谢。
不好意思,今天在看到。 我说的乱码问题是因为我使用的是日文服务器,在本机连接的时候会出现乱码,导致的连接失败。 现在已经在jsch包中自己添加了字符集转换的方法,不过不知道你是否是相同的问题。
zyqin 2013-07-17
  • 打赏
  • 举报
回复
请问楼主,“乱码问题”具体指的是什么问题?我也遇到这个问题,可以登录linux,但是登录solaris的时候报错“Auth fail“。谢谢。
土豆你个番茄 2013-05-27
  • 打赏
  • 举报
回复
已解决 乱码问题
土豆你个番茄 2013-05-22
  • 打赏
  • 举报
回复
用户名和密码是没有错的 但是一直验证失败

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧