1. 程式人生 > 其它 >Java使用Jcdh連線伺服器(採用密碼登陸方式)

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(); } } }