package com.elco.util; import com.jcraft.jsch.*; /** * 判断文件是否存在(工具) */ public class FileUtils { public static final String SFTP_PROTOCAL = "sftp"; /** * 创建SFTP服务器连接session * * @param jsch 需要用到jsch-0.1.54.jar包 * @param host IP地址 * @param username 用户名 * @return session * @throws Exception */ private static Session createSession(JSch jsch, String host, String username) throws Exception { int port = 0; Session session = null; if (port <= 0) { // 连接服务器,使用默认端口 session = jsch.getSession(username, host); } else { // 服务器采用指定的端口连接 session = jsch.getSession(username, host, port); } // 若服务器无法连接,则抛出异常 if (session == null) { throw new Exception(host "session is null"); } // 第一次登录时设置提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); // session.setConfig("kex", "diffie-hellman-group1-sha1"); return session; } /** * 创建SFTP连接 * * @param host 主机IP * @param username 主机登录用户名 * @param password 主机登录密码 * @return SFTP * @throws Exception */ public static ChannelSftp connect(String host, String username, String password) throws Exception { Channel channel = null; ChannelSftp sftp = null; JSch jsch = new JSch(); // 获取连接session Session session = createSession(jsch, host, username); // 设置登录主机的密码 session.setPassword(password); // 设置超时登录时间 session.connect(15000); System.out.println("Session connected to " host "."); try { // 创建SFTP通信通道 channel = (Channel) session.openChannel(SFTP_PROTOCAL); channel.connect(1000); System.out.println("Channel created to " host "."); sftp = (ChannelSftp) channel; } catch (JSchException e) { System.out.println("exception when channel create."); } return sftp; } /** * 创建文件目录 */ public static void createDir(String createpath, ChannelSftp sftp) { try { if (isDirExist(createpath, sftp)) { sftp.cd(createpath); return; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path "/"); if (isDirExist(filePath.toString(), sftp)) { sftp.cd(filePath.toString()); } else { // 建立目录 sftp.mkdir(filePath.toString()); // 进入并设置当前目录 // sftp.cd(filePath.toString()); } } // sftp.cd(createpath); } catch (SftpException e) { System.out.println("创建目录失败"); } } /** * 判断目录是否存在 */ public static boolean isDirExist(String directory, ChannelSftp sftp) { boolean isDirExistFlag = false; try { SftpATTRS sftpATTRS = sftp.lstat(directory); isDirExistFlag = true; return sftpATTRS.isDir(); } catch (Exception e) { if (e.getMessage().toLowerCase().equals("no such file")) { isDirExistFlag = false; } } return isDirExistFlag; } /** * 判断文件是否存在,不存在时,创建文件 * * @param directory * @param host * @param username * @param password * @throws Exception */ public static void isfile(String directory, String host, String username, String password) throws Exception { Channel channel = null; ChannelSftp sftp = null; JSch jsch = new JSch(); // 获取连接session Session session = createSession(jsch, host, username); // 设置登录主机的密码 session.setPassword(password); // 设置超时登录时间 session.connect(15000); System.out.println("Session connected to " host "."); try { // 创建SFTP通信通道 channel = (Channel) session.openChannel(SFTP_PROTOCAL); channel.connect(1000); System.out.println("Channel created to " host "."); sftp = (ChannelSftp) channel; createDir(directory, sftp); } catch (JSchException e) { System.out.println("exception when channel create."); } //关闭 sftp.disconnect(); channel.disconnect(); session.disconnect(); jsch = null; } }