ftp与sftp连接实例
来源:互联网 发布:大数据方向好不好 编辑:程序博客网 时间:2024/06/05 04:37
1.ftp连接方式
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; public class Test1 { public static FTPClient ftpClient = new FTPClient(); public static boolean connect(String hostname, String username, String password) throws IOException { ftpClient.connect(hostname); ftpClient.setControlEncoding("UTF-8"); if (ftpClient.login(username, password)) { ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); System.out.println("FTP连接成功!"); return true; } System.out.println("FTP连接失败!"); disconnect(); return false; } public static void disconnect() throws IOException { if (ftpClient.isConnected()) { ftpClient.disconnect(); System.out.println("FTP disconnected.."); } } public static void main(String[] args) { try { String hostname = "10.193.22.146"; String username = "test"; String password = "test"; connect(hostname, username, password); disconnect(); }catch (Exception e) { e.printStackTrace(); } } }
2.sftp连接方式
import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class Test { private ChannelSftp sftp; /** * @param args * @throws InterruptedException * @throws ParseException * @throws IOException */ public static void main(String[] args) throws InterruptedException, ParseException, Exception { sftp = connect("10.198.12.87", 22, "admin", "admin"); sftp.disconnect(); } /** * 连接sftp服务器 * * @param host 主机 * @param port 端口 * @param username 用户名 * @param password 密码 * @return */ public static ChannelSftp connect(String host, int port, String username, String password) { ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); System.out.println("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println("Session connected."); System.out.println("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("Connected to " + host + "."); } catch (Exception e) { e.printStackTrace(); } return sftp; } public ChannelSftp getSftp() { return sftp; } public void setSftp(ChannelSftp sftp) { this.sftp = sftp; } }
备注:
1.ftp连接不安全,一般出于安全性的考虑会关闭ftp端口,连接ftp都是通过sftp方式连接;
2.ftp的上传与下载需要设置文件传输模式为二进制传输模式,如:
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
阅读全文
0 0
- ftp与sftp连接实例
- ftp与sftp连接实例
- ftp与sftp
- sftp与ftp用法
- FTP与SFTP
- FTP 与SFTP区别
- fileZilla错误:无法与 SFTP 服务器建立 FTP 连接
- 错误:无法与 SFTP 服务器建立 FTP 连接
- 错误:无法与 SFTP 服务器建立 FTP 连接
- Sublime Text 连接 FTP/SFTP
- FileZilla(ftp )错误:无法与 SFTP 服务器建立 FTP 连接 解决方案
- FTP与SFTP的区别
- FTP与SFTP的区别
- FTP与SFTP的区别
- FTP与SFTP的区别
- FTP与SFTP的区别
- Sublime Text 如何连接 FTP/SFTP
- Sublime Text 如何连接 FTP/SFTP
- Map集合/Exception类/File类
- Android进程管理
- 我的学习记录41
- 编程应该养成哪些好的习惯
- 11月16日笔记及心得
- ftp与sftp连接实例
- 刘汝佳p35,2-6(123排列)算法竞赛入门经典第二版
- birt报表学习
- 服务计算系列——gzip中间件分析
- golang-redis之string类型简单操作
- 《设计模式之禅》读书笔记(四)之抽象工厂模式
- Ubuntu16.04中切换Python2/3,设置为Python3为默认
- 小中型业务常用数据库优
- C++学习之C语言基础篇