• SFTP(Secure File Transfer Protocol)的文件下载和上传


    JSch库来实现SFTP(Secure File Transfer Protocol)的文件下载和上传

    import com.jcraft.jsch.*;
    public class SftpExample {
    	public static void main(String[] args) {
    		String host = "your_sftp_host";
    		int port = 22;
    		String username = "your_username";
    		String password = "your_password";
    		// 文件下载 
    		String remoteFilePath = "/remote/path/to/file.txt";
    		String localFilePath = "local/path/to/save/file.txt";
    		downloadFile(host, port, username, password, remoteFilePath, localFilePath);
    		// 文件上传
    		String localFileToUpload = "local/path/to/upload/file.txt";
    		String remoteDirectory = "/remote/path/to/upload/";
    		uploadFile(host, port, username, password, localFileToUpload, remoteDirectory);
    	}
    	public static void downloadFile(String host, int port, String username, String password, String remoteFilePath, String localFilePath) {
    		try {
    			JSch jsch = new JSch();
    			Session session = jsch.getSession(username, host, port);
    			session.setPassword(password);
    			session.setConfig("StrictHostKeyChecking", "no");
    			session.connect();
    			ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
    			channelSftp.connect();
    			channelSftp.get(remoteFilePath, localFilePath);
    			channelSftp.disconnect();
    			session.disconnect();
    			System.out.println("File downloaded successfully.");
    		}
    		catch (JSchException | SftpException e) {
    			e.printStackTrace();
    		}
    	}
    	public static void uploadFile(String host, int port, String username, String password, String localFilePath, String remoteDirectory) {
    		try {
    			JSch jsch = new JSch();
    			Session session = jsch.getSession(username, host, port);
    			session.setPassword(password);
    			session.setConfig("StrictHostKeyChecking", "no");
    			session.connect();
    			ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
    			channelSftp.connect();
    			channelSftp.put(localFilePath, remoteDirectory);
    			channelSftp.disconnect();
    			session.disconnect();
    			System.out.println("File uploaded successfully.");
    		}
    		catch (JSchException | SftpException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    
    import com.jcraft.jsch.*;
    public class SftpExample {
    	public static void main(String[] args) {
    		String host = "your_sftp_host";
    		int port = 22;
    		String username = "your_username";
    		String password = "your_password";
    		// 文件夹下载 
    		String remoteFolderPath = "/remote/path/to/folder";
    		String localFolderPath = "local/path/to/save/folder";
    		downloadFolder(host, port, username, password, remoteFolderPath, localFolderPath);
    	}
    	public static void downloadFolder(String host, int port, String username, String password, String remoteFolderPath, String localFolderPath) {
    		try {
    			JSch jsch = new JSch();
    			Session session = jsch.getSession(username, host, port);
    			session.setPassword(password);
    			session.setConfig("StrictHostKeyChecking", "no");
    			session.connect();
    			ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
    			channelSftp.connect();
    			// 创建本地文件夹
    			java.nio.file.Path localPath = java.nio.file.Paths.get(localFolderPath);
    			java.nio.file.Files.createDirectories(localPath);
    			// 下载文件夹 
    			channelSftp.get(remoteFolderPath + "/*", localFolderPath);
    			channelSftp.disconnect();
    			session.disconnect();
    			System.out.println("Folder downloaded successfully.");
    		}
    		catch (JSchException | SftpException | java.io.IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
  • 相关阅读:
    Jmeter自带函数不够用?不如自己动手开发一个
    看图学习数据中心机柜导轨方面的英文术语
    企业常用Linux三剑客awk及案例/awk底层剖析/淘宝网cdn缓存对象分级存储策略案例/磁盘知识/awk统计与计算-7055字
    Sonar: static 修饰符顺序违法了JLS建议
    动态规划算法
    【软考】-- 多媒体基础知识
    罗克韦尔AB PLC Logix5000中如何创建标签并使用标签进行编程?
    Maven进阶
    Python日期时间差的计算(天/小时/分钟)及timedelta函数的使用(附python代码)
    Linux生产者消费者模型
  • 原文地址:https://blog.csdn.net/weixin_38032826/article/details/134397272