java fttp連線伺服器操作
阿新 • • 發佈:2018-12-12
*1.匯入jsch-0.1.51.jar包 2.建立SFTP封裝類:
import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.Session; public class SFTP{ private Session session;//會話 private Channel channel;//連線通道 private ChannelSftp sftp;// sftp操作類 public Session getSession() { return session; } public void setSession(Session session) { this.session = session; } public Channel getChannel() { return channel; } public void setChannel(Channel channel) { this.channel = channel; } public ChannelSftp getSftp() { return sftp; } public void setSftp(ChannelSftp sftp) { this.sftp = sftp; } }
3.建立工具操作類:
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.Vector; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SFTPUtil { private static Logger log = LogManager.getLogger(SFTPUtil.class); /** * 連線ftp/sftp伺服器 * @param SFTP類 */ public static void getConnect(SFTP s) throws Exception { /** 金鑰的密碼 */ // String privateKey ="key"; // /** 金鑰檔案路徑 */ // String passphrase ="path"; /** 主機 */ String host =""; //伺服器ip地址 /** 埠 */ int port =22; /** 使用者名稱 */ String username =""; // 伺服器使用者名稱,密碼 /** 密碼 */ String password =""; Session session = null; Channel channel = null; ChannelSftp sftp = null;// sftp操作類 JSch jsch = new JSch(); //設定金鑰和密碼 //支援金鑰的方式登陸,只需在jsch.getSession之前設定一下金鑰的相關資訊就可以了 // if (privateKey != null && !"".equals(privateKey)) { // if (passphrase != null && "".equals(passphrase)) { // //設定帶口令的金鑰 // jsch.addIdentity(privateKey, passphrase); // } else { // //設定不帶口令的金鑰 // jsch.addIdentity(privateKey); // } // } session = jsch.getSession(username, host, port); session.setPassword(password); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); // 不驗證 HostKey session.setConfig(config); try { session.connect(); } catch (Exception e) { if (session.isConnected()) session.disconnect(); log.error("連線伺服器失敗,請檢查主機[" + host + "],埠[" + port + "],使用者名稱[" + username + "],埠[" + port + "]是否正確,以上資訊正確的情況下請檢查網路連線是否正常或者請求被防火牆拒絕."); } channel = session.openChannel("sftp"); try { channel.connect(); } catch (Exception e) { if (channel.isConnected()) channel.disconnect(); log.error("連線伺服器失敗,請檢查主機[" + host + "],埠[" + port + "],使用者名稱[" + username + "],密碼是否正確,以上資訊正確的情況下請檢查網路連線是否正常或者請求被防火牆拒絕."); } sftp = (ChannelSftp) channel; s.setChannel(channel); s.setSession(session); s.setSftp(sftp); } /** * 斷開連線 * */ public static void disConn(Session session,Channel channel,ChannelSftp sftp)throws Exception{ if(null != sftp){ sftp.disconnect(); sftp.exit(); sftp = null; } if(null != channel){ channel.disconnect(); channel = null; } if(null != session){ session.disconnect(); session = null; } } /** * 進入目錄 * @param directory * @throws Exception */ public static void cd(String directory)throws Exception { SFTP s=new SFTP(); getConnect(s);//建立連線 Session session = s.getSession(); Channel channel = s.getChannel(); ChannelSftp sftp = s.getSftp();// sftp操作類 try { sftp.cd(directory); //目錄要一級一級進 } catch (Exception e) { throw new Exception(e.getMessage(),e); } finally { disConn(session,channel,sftp); } } /** * 列出目錄下的檔案 * @param directory 要列出的目錄 * @return list 檔名列表 * @throws Exception */ public static List<String> listFiles(String directory) throws Exception { SFTP s=new SFTP(); getConnect(s);//建立連線 Session session = s.getSession(); Channel channel = s.getChannel(); ChannelSftp sftp = s.getSftp();// sftp操作類 Vector fileList=null; List<String> fileNameList = new ArrayList<String>(); fileList = sftp.ls(directory); //返回目錄下所有檔名稱 disConn(session,channel,sftp); Iterator it = fileList.iterator(); while(it.hasNext()) { String fileName = ((LsEntry)it.next()).getFilename(); if(".".equals(fileName) || "..".equals(fileName)){ continue; } fileNameList.add(fileName); } return fileNameList; }
}