java程序实现对sftp服务器的操作

本文介绍了使用JSch-JavaSecureChannel框架进行SFTP文件上传、下载及删除等操作的具体实现方法。通过设置连接参数,如主机名、用户名、密码等,建立SFTP连接,并演示了如何利用该框架完成文件传输任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里介绍对sftp操作的一种java框架:JSch-Java Secure Channel,官方地址是:http://www.jcraft.com/jsch/

具体使用方法请看下面代码:

package jsch;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class Test {

	protected String host;//sftp服务器ip
	protected String username;//用户名
	protected String password;//密码
	protected String privateKey;//密钥文件路径
	protected String passphrase;//密钥口令
	protected int port = 22;//默认的sftp端口号是22

	/**
	 * 获取连接
	 * @return channel
	 */
	public ChannelSftp connectSFTP() {
		JSch jsch = new JSch();
		Channel channel = null;
		try {
			if (privateKey != null && !"".equals(privateKey)) {
				//使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥
				if (passphrase != null && "".equals(passphrase)) {
					jsch.addIdentity(privateKey, passphrase);
				} else {
					jsch.addIdentity(privateKey);
				}
			}
			Session session = jsch.getSession(username, host, port);
			if (password != null && !"".equals(password)) {
				session.setPassword(password);
			}
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");// do not verify host key
			session.setConfig(sshConfig);
			// session.setTimeout(timeout);
			session.setServerAliveInterval(92000);
			session.connect();
			//参数sftp指明要打开的连接是sftp连接
			channel = session.openChannel("sftp");
			channel.connect();
		} catch (JSchException e) {
			e.printStackTrace();
		}
		return (ChannelSftp) channel;
	}
	
	/**
	 * 上传文件
	 * 
	 * @param directory
	 *            上传的目录
	 * @param uploadFile
	 *            要上传的文件
	 * @param sftp
	 */
	public void upload(String directory, String uploadFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			File file = new File(uploadFile);
			sftp.put(new FileInputStream(file), file.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param directory
	 *            下载目录
	 * @param downloadFile
	 *            下载的文件
	 * @param saveFile
	 *            存在本地的路径
	 * @param sftp
	 */
	public void download(String directory, String downloadFile,
			String saveFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.get(downloadFile,saveFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param directory
	 *            要删除文件所在目录
	 * @param deleteFile
	 *            要删除的文件
	 * @param sftp
	 */
	public void delete(String directory, String deleteFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void disconnected(ChannelSftp sftp){
		if (sftp != null) {
			try {
				sftp.getSession().disconnect();
			} catch (JSchException e) {
				e.printStackTrace();
			}
			sftp.disconnect();
		}
	}
}

 


在jsch自带的例子中,有一个可以根据密钥生成公钥的类,叫做UserAuthPubKey.java, 且带有图形界面。有用到的可以自己试试。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值