ftp与sftp连接实例
来源:互联网 发布:sd卡数据修复 编辑:程序博客网 时间:2024/06/05 11:06
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
- 二分查找
- C/C++常用宏定义
- Form表单中的action提交路径问题
- C++中struct和class的区别
- Gulp使用指南
- ftp与sftp连接实例
- How to Support 3D Touch - Static Action on Xcode6
- Android基础入门教程——8.1.2 Android中的13种Drawable小结 Part 2
- Code Forces 581 C. Developing Skills(贪心)
- C++primer第五版笔记-第九章顺序容器
- DBus 进程间的通信
- LeetCode-Invert Binary Tree
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- 什么是EMC?说出至少三种测试实验项目,如何解决