Java使用Jcdh连接服务器(采用密码登陆方式)
工具类:
package com.dpi.util; import com.jcraft.jsch.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.List; import java.util.Properties; import java.util.Vector; public class SftpUtil { private static Logger log = LoggerFactory.getLogger(SftpUtil.class); public static Session getSession(String host, int port, String username, String password) { Session sshSession = null; try { JSch jsch = new JSch(); //添加私钥路径 // jsch.addIdentity(priKey_base_path); jsch.getSession(username, host, port); sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); log.info("sshSession连接成功!"); } catch (Exception e) { log.info("sshSession连接异常,"+e.toString()); e.printStackTrace(); } return sshSession; }
public static ChannelSftp connectSftp(Session session) { ChannelSftp sftp = null; try { log.info("Opening Channel."); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (Exception e) { log.info("Sftp连接异常,"+e.toString()); e.printStackTrace(); sftp = null; } return sftp; } /** * @Author lizhm * @Description 上传文件 * @Date 11:36 2021/8/31 * @Param [directory, uploadFile, sftp] * @return void * @History: <author> <time> <version> <desc> **/ public static void upload(String fileDir, String uploadFile, ChannelSftp sftp) { try { if(!isDirExist(sftp, fileDir)){ sftp.mkdir(fileDir); } sftp.cd(fileDir); File file = new File(uploadFile); if(!file.exists()){ log.info("未找到待分发的IP配置文件!"); return; } FileInputStream fileInputStream = new FileInputStream(file); sftp.put(fileInputStream, file.getName()); fileInputStream.close(); } catch (Exception e) { log.info("分发IP配置文件异常,"+e.toString()); e.printStackTrace(); } }
public static Vector listFiles(String directory, ChannelSftp sftp)throws SftpException { return sftp.ls(directory); } public void renameFile(String directory, String oldname, String newname,ChannelSftp sftp) { try { sftp.cd(directory); sftp.rename(oldname, newname); } catch (Exception e) { e.printStackTrace(); } } public static void deleteAllTxtBySftp(String fileDir, ChannelSftp sftp) { try { if(!isDirExist(sftp, fileDir)){ log.info("源服务器的IP配置文件路径不存在!"); return ; } sftp.cd(fileDir); // 获取目录下面所有文件 Vector nVector = sftp.ls(fileDir); // 循环遍历文件 for (int i = 0; i < nVector.size(); i++) { String nFileName = nVector.get(i).toString().substring(56, nVector.get(i).toString().length()); if (nFileName.toLowerCase().endsWith(".txt")) { sftp.rm(nFileName); } } } catch (Exception e) { log.info("删除源IP配置文件失败,"+e.toString()); e.printStackTrace(); } } public static List<String> downloadAllTxt(String viDirectory, ChannelSftp sftp, String viSaveDir) { List<String> nFileNameList = null; try { if(!isDirExist(sftp, viDirectory)){ log.info("源服务器的IP配置文件路径不存在!"); return nFileNameList; } // 获取目录下面所有文件 Vector nVector = sftp.ls(viDirectory); // 循环遍历文件 for (int i = 0; i < nVector.size(); i++) { // 进入服务器文件夹 sftp.cd(viDirectory); // 实例化文件对象 String nFileName = nVector.get(i).toString().substring(56, nVector.get(i).toString().length()); if (!nFileName.toLowerCase().endsWith(".txt")) { continue; } File nFile = new File(viSaveDir + File.separator + nFileName); // 下载文件 FileOutputStream fileOutputStream = new FileOutputStream(nFile); sftp.get(nFileName, fileOutputStream); fileOutputStream.close(); } } catch (Exception e) { log.info("源服务器IP配置文件下载异常,"+e.toString()); e.printStackTrace(); } return nFileNameList; } public static boolean isDirExist(ChannelSftp channelSftp, String fileDir) { try { SftpATTRS sftpATTRS = channelSftp.lstat(fileDir); return sftpATTRS.isDir(); } catch (SftpException e) { return false; } } public static void mkDir(ChannelSftp sftp, String fileDir) { try { if (!isDirExist(sftp, fileDir)) { sftp.mkdir(fileDir); } } catch (Exception e) { log.info("创建目录异常,"+e.toString()); e.printStackTrace(); } } public static void bakTxt(String fromDir, String toDir, ChannelSftp sftp, Session session) { try { if(!isDirExist(sftp, fromDir)){ log.info("源服务器的IP配置文件路径不存在!"); return ; } if(!isDirExist(sftp, toDir)){ sftp.mkdir(toDir); } String cmd = "cp -R " + fromDir + "/*.[T,t][X,x][T,t] " + toDir; execCommand(session, cmd); } catch (Exception e) { log.info("备份源IP配置文件失败,"+e.toString()); e.printStackTrace(); } } public static void deleteAllTxt(String fileDir, Session session) { try { String cmd = "rm -rf " + fileDir + "/*.[T,t][X,x][T,t] "; execCommand(session, cmd); } catch (Exception e) { log.info("删除源IP配置文件失败,"+e.toString()); e.printStackTrace(); } } public static void moveAllTxt(String fromDir, String toDir, ChannelSftp sftp, Session session) { try { if(!isDirExist(sftp, fromDir)){ log.info("源服务器的IP配置文件路径不存在!"); return ; } if(!isDirExist(sftp, toDir)){ sftp.mkdir(toDir); } String cmd = "mv -f " + fromDir + "/*.[T,t][X,x][T,t] " + toDir; execCommand(session, cmd); } catch (Exception e) { log.info("备份源IP配置文件失败,"+e.toString()); e.printStackTrace(); } } public static String execCommand(Session session, String command){ try{ ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); InputStream in = channelExec.getInputStream(); channelExec.setCommand(command); channelExec.setErrStream(System.err); channelExec.connect(); // out = IOUtils.toString(in, "UTF-8"); in.close(); channelExec.disconnect(); session.disconnect(); }catch (Exception e){ log.info(e.toString()); e.printStackTrace(); } return ""; } public static void close(ChannelSftp sftp, Session session) { if (sftp != null) { sftp.disconnect(); sftp.exit(); } if (session != null) { session.disconnect(); } } public static void closeChannel(ChannelSftp sftp) { if (sftp != null) { sftp.disconnect(); sftp.exit(); } } }