用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)
JavaSFTP Auth fail
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)
JavaSFTP Auth fail
解决方案 »
- IE 加载java applet 失败,为什么
- 字符数组排序
- java对oracle数据库备份
- JTABLE选择行的设置问题,急
- 关于接口!!请java2000_net讲解一下!
- resin如何使用struts,在eclipse里面怎么创建这样的一个工程能在eclipse里面启动resin,并调试类和页面
- 帮我看看我的问题~~~~~
- 求JAVA虚拟机资料
- 加密解密程序中遇到的困惑问题?含样例程序,求解决!谢了!
- 为什么转义字符“\n”写到文本文件中是一个黑方块?而不是换行?不解!!!,应怎么写才是换行?
- 怎么删除JTree中选中的节点(多个)
- Swing更新界面问题:TextArea载入大文本文件GUI界面失去响应
不好意思,今天在看到。
我说的乱码问题是因为我使用的是日文服务器,在本机连接的时候会出现乱码,导致的连接失败。
现在已经在jsch包中自己添加了字符集转换的方法,不过不知道你是否是相同的问题。