java:sftp推送⽂件⾄服务器1、需要依赖:
<!-- mvnrepository/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
2、判断⽬标服务器 需要⽂件夹是否存在,不存在则创建创建
* 检查创建多层⽂件夹
*
* @param sftp
* @param path
repository文件夹可以删除吗*/
public static void checkDirsIsExsit(ChannelSftp sftp, String path) {
try {
sftp.cd(path);
} catch (Exception e) {
// TODO Auto-generated catch block
String[] dirName = path.split("/");
String t = "";
for (int i = 0; i < dirName.length; i++) {
t = t + "/" + dirName[i];
checkDirIsExsit(sftp, t);
}
}
}
/**
* ftp创建单个⽂件夹
*
* @param sftp
* @param path
*/
public static void checkDirIsExsit(ChannelSftp sftp, String path) {  try {
sftp.cd(path);
} catch (Exception e) {
// TODO Auto-generated catch block
try {
System.out.println("新建⽂件夹:" + path);
sftp.mkdir(path);
} catch (SftpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
3、sftp 推送⽂件之服务器
* 推送⽂件
*
* @param cfg
* @param src
* @param dst
* @throws JSchException
*/
public static void ftpPut(String userName, String host, int port, String passWord, String src, String dst)  throws JSchException {
JSch jsch = new JSch();
Session session = Session(userName, host, port);
session.setPassword(passWord);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig("userauth.gssapi-with-mic", "no");
session.setConfig(sshConfig);
ChannelSftp ftp = (ChannelSftp) session.openChannel("sftp");
checkDirsIsExsit(ftp, dst);
File dir = new File(src);
String[] list = dir.list();
for (String str : list) {
try {
ftp.AbsolutePath()+"/"+str, dst + "/"+str);
System.out.println("推送:"+str+"完毕!!");
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ftp.disconnect();
session.disconnect();
}
推送程序⾄此完毕