Java用SFTP实现上传下载功能

请先提前下载jar包jsch-0.1.54.jar,官网下载地址

http://www.jcraft.com/jsch/

https://repo1.maven.org/maven2/com/jcraft/jsch/0.1.55/

 

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

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

/**
 * SFTP工具类
 */
public class SFTPUtils {
	static Session sshSession = null;

	public static void main(String[] args) {
		ChannelSftp ftp = getConnectIP("223.105.1.30", "22", "username", "password");
		upload("/home/cmsoftu/detail/", "E:\\recordfilecopy\\record1.txt", ftp);
		// download("/home/cmsoftu/detail_history/", "record1.txt", "E:\\recordfile\\record1.txt", ftp);

	}

	/**
	 * 获取ChannelSftp gst:2017-9-12
	 * 
	 * @param host
	 *            主机
	 * @param sOnlineSftpPort
	 *            端口
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return
	 */
	public static ChannelSftp getConnectIP(String host, String sOnlineSftpPort, String username, String password) {
		int port = 0;
		if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {
			port = Integer.parseInt(sOnlineSftpPort);
		}
		ChannelSftp sftp = null;
		try {
			JSch jsch = new JSch();
			jsch.getSession(username, host, port);
			sshSession = jsch.getSession(username, host, port);
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			sshSession.connect();
			Channel channel = sshSession.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sftp;
	}

	/**
	 * 上传 gst:2017-9-12
	 * 
	 * @param directory
	 *            sftp 服务器目录
	 * @param uploadFile
	 *            上传文件路径
	 * @param sftp
	 */
	public static 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();
		} finally {
			if (sftp.isConnected()) {
				sshSession.disconnect();
				sftp.disconnect();
			}

		}
	}

	/**
	 * 下载 gst:2017-9-12
	 * 
	 * @param directory
	 *            sftp服务器目录
	 * @param downloadFile
	 *            目录下的文件名称
	 * @param saveFile
	 *            本地保存文件路径
	 * @param sftp
	 */
	public static void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			File file = new File(saveFile);
			sftp.get(downloadFile, new FileOutputStream(file));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sftp.isConnected()) {
				sshSession.disconnect();
				sftp.disconnect();
			}

		}
	}

	/**
	 * 删除 gst:2017-9-12
	 * 
	 * @param directory
	 * @param deleteFile
	 * @param sftp
	 */
	public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sftp.isConnected()) {
				sshSession.disconnect();
				sftp.disconnect();
			}

		}
	}

}

 

 

 

 

 

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值