java中使用jsch向服务器传输文件或文件夹

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

JSch是SSH2的一个纯Java实现。JSch允许你连接到一个sshd服务器,用来端口转发,X11转发,文件传输等等。可以将它的功能集成到自己的Java程序中。JSch也有对应支持J2ME的版本, 但不在本文范围内, 感兴趣的可以自行研究


一、JSch下载及使用方法

1. jar包下载

官网地址 http://www.jcraft.com/jsch/下载链接在该页面中部

2. 添加maven依赖

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.55</version>
</dependency>

二、代码样例(传输单个文件或文件夹至服务器指定目录下)

里面的注释很详细, 不再多做说明:

import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

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

/**
 * 
 * @author yuguagua
 * @email feimeideyanggao@126.com
 * @date 2021年2月5日 下午3:50:28
 * @company 哈尔滨亿时代数码科技开发有限公司
 */
public class SFTPChannelUtils {

	Session session = null;
	Channel channel = null;

	// 获取sftp channel对象
	public ChannelSftp getChannelSftp() throws JSchException {
		// ssh服务器的IP、用户名、密码和端口
		String sftpHost = "192.168.1.241", sftpUsername = "username", sftpPassword = "password";
		int sftpPort = 22;

		JSch jsch = new JSch(); // 创建JSch对象
		System.out.println(" 创建JSch对象");
		session = jsch.getSession(sftpUsername, sftpHost, sftpPort);// 获取sesson对象
		System.out.println("获取sesson对象");
		session.setPassword(sftpPassword);// 设置sftp访问密码

		Properties config = new Properties();
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config);// 为session重新设置参数
		session.setTimeout(30000);// 设置超时
		System.out.println("相关连接参数设置完毕, 准备建立连接");
		session.connect();// 建立连接
		System.out.println("建立连接");

		channel = session.openChannel("sftp"); // 打开sftp通道
		System.out.println("打开sftp通道");
		channel.connect();// 建立sftp通道连接
		System.out.println("建立sftp通道连接");
		return (ChannelSftp) channel;
	}

	public void closeChannel() {
		if (channel != null) {
			channel.disconnect();
			System.out.println("断开channel");
		}
		if (session != null) {
			session.disconnect();
			System.out.println("断开session");
		}
	}
	
	public static void main(String[] args) {

		try {
			// JSch支持三种文件传输模式:
			// OVERWRITE 完全覆盖模式,这是JSch的 默认
			// 文件传输模式,即如果目标文件已经存在,传输的文件将完全覆盖目标文件,产生新的文件。
			// RESUME 恢复模式,如果文件已经传输一部分,这时由于网络或其他任何原因导致文件传输中断,如果下一次传输相同的文件,
			// 则会从上一次中断的地方续传。
			// APPEND 追加模式,如果目标文件已存在,传输的文件将在目标文件后追加。
			SFTPChannelUtils sftpChannel = new SFTPChannelUtils();
			ChannelSftp channel = sftpChannel.getChannelSftp();
			/**	3种传输单个文件的方法	begin**/
			String srcFile = "d:\\testfile.rar", destFile = "/home/cbj/yuguagua/tmp/testfile.rar";
			// String srcFile = "d:\\jquery-ui-1.10.4.custom.js", destFile =
			// "/var/www/html/jquery-ui-1.10.4.custom.js";
			System.out.println("准备传输文件");
			// 以下使用3种put方法来传输文件
//			// 方法 1/3
//			OutputStream out = channel.put(destFile, ChannelSftp.OVERWRITE);
//			// 默认使用OVERWRITE模式
//			byte[] buff = new byte[1024 * 256]; // 设定每次传输的数据块大小为256KB
//			int read;
//			if (out != null) {
//				System.out.println("Start to read input stream");
//				InputStream is = new FileInputStream(srcFile);
//				do {
//					read = is.read(buff, 0, buff.length);
//					if (read > 0) {
//						out.write(buff, 0, read);
//					}
//					out.flush();
//				} while (read >= 0);
//			}

			// 方法 2/3
			// channel.put(srcFile, destFile, ChannelSftp.APPEND);

			// 方法 3/3 
			// channel.put(new FileInputStream(srcFile), destFile,
			// ChannelSftp.OVERWRITE);
			
			/**	3种传输单个文件的方法	end **/
			
			
			/** 传输文件夹到指服务器指定目录下	begin	**/
			String srcDirectory = "d:\\Program Files\\", destDirectory = "/home/cbj/yuguagua/tmp/test";
			sftpChannel.chuanshu(channel, srcDirectory, destDirectory);
			/** 传输文件夹到指服务器指定目录下	end	**/
			
			System.out.println("传输完成");
			channel.quit();
			sftpChannel.closeChannel();

		} catch (Exception e) {
			e.printStackTrace();
			// } catch (FileNotFoundException e) {
			// e.printStackTrace();
			// } catch (IOException e) {
			// e.printStackTrace();
		}

	}

	/**
	 * 向服务器指定目录下以覆盖模式传输文件|文件夹
	 * 
	 * @author yuguagua
	 * @email feimeideyanggao@126.com
	 * @create 2021年2月6日
	 * @param channel
	 *            sftp通道对象
	 * @param 源文件[目录]路径,
	 *            可为文件或文件目录
	 * @destFilePath 目标文件夹目录, 必须存在在且必须为目录!!!不可以是文件!!!, 后缀不要加 / , 传输的文件夹会新建在此目录下
	 * @return
	 */
	public void chuanshu(ChannelSftp channel, String srcFilePath, String destFilePath) {
		File file = new File(srcFilePath);
		String subDestFilePath = destFilePath + "/" + file.getName(); // 真·文件目录
		// 1. 源文件为目录, 则列表显示其下所有的文件夹、文件
		if (file.isDirectory()) {
			// 1.1 在destFilePath目录下检查本目录是否存在, 存在则删除再创建; 不存在则新创建
			try {
				channel.cd(subDestFilePath);
				System.out.println("目标服务器上 目录: " + subDestFilePath + " 存在, 先予以删除");
				channel.cd(destFilePath);
				deleteTargetFile(channel, subDestFilePath);
				System.out.println("目标服务器上 目录: " + subDestFilePath + " 删除成功");
				channel.mkdir(subDestFilePath);
				System.out.println("目标服务器上 目录: " + subDestFilePath + " 删除后创建成功");
			} catch (SftpException e) {
				System.out.println("目标服务器上 目录: " + subDestFilePath + " 不存在, 正在创建");
				try {
					channel.mkdir(subDestFilePath);
				} catch (SftpException e1) {
					System.out.println("目标服务器上 创建目录: " + subDestFilePath + " 出错." + e1.getLocalizedMessage());
				}
			}
			File subfiles[] = file.listFiles();
			for (int i = 0; i < subfiles.length; i++) {
				chuanshu(channel, subfiles[i].getAbsolutePath(), subDestFilePath);
			}
		} else {
			// 2. 源文件为文件,将文件传输到 destFilePath目录下
			// // 2.1 检查destFilePath目录是否存在, 不存在则创建
			// try {
			// channel.cd(destFilePath);
			// } catch (SftpException e) {
			// System.out.println("目录: " + destFilePath + " 不存在, 正在创建");
			// try {
			// channel.mkdir(destFilePath);
			// } catch (SftpException e1) {
			// System.out.println("创建目录: " + destFilePath + " 出错." +
			// e1.getLocalizedMessage());
			//
			// }
			// }
			try {
				channel.cd(destFilePath);
				channel.put(srcFilePath, destFilePath + "/" + file.getName(), ChannelSftp.OVERWRITE);
				System.out.println("传输文件: " + srcFilePath + "  到  " + destFilePath + "/" + file.getName() + "  成功.");
			} catch (SftpException e) {
				System.out.println("传输文件: " + srcFilePath + "  到  " + destFilePath + "/" + file.getName() + "  出错."
						+ e.getLocalizedMessage());
			}
		}

	}

	/**
	 * 删除服务器指定的文件夹及其里面的所有内容
	 * 
	 * @author yuguagua
	 * @email feimeideyanggao@126.com
	 * @create 2021年2月6日
	 * @param targetFilePath
	 *            要删除的文件或文件夹路径, 应为绝对路径, 且非空
	 * @return
	 */
	public void deleteTargetFile(ChannelSftp channel, String targetFilePath) {
		try {
			// 1. 路径为文件夹
			if (channel.stat(targetFilePath).isDir()) {
				channel.cd(targetFilePath);
				Vector<LsEntry> entries = channel.ls(".");
				for (LsEntry subFilePath : entries) {
					String fileName = subFilePath.getFilename();
					if (".".equals(fileName) || "..".equals(fileName)) {
						continue;
					}
					deleteTargetFile(channel, subFilePath.getFilename());
				}
				channel.cd("..");
				channel.rmdir(targetFilePath);
				System.out.println("删除文件夹: " + targetFilePath + " 成功. ");
			} else {
				// 2. 路径为文件
				try {
					channel.rm(targetFilePath);
					System.out.println("删除文件: " + targetFilePath + " 成功. ");
				} catch (SftpException e) {
					System.out.println("删除文件: " + targetFilePath + " 出错. " + e.getMessage());
				}
			}
		} catch (SftpException e) {
			System.out.println("判断文件: " + targetFilePath + " 类型出错. " + e.getMessage());
		}

	}
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java使用jsch库进行远程文件下载可以实现通过SSH协议从远程服务器下载文件。下面是一个简单的示例代码: ```java import com.jcraft.jsch.*; public class RemoteFileDownloader { public static void main(String[] args) { String host = "远程服务器IP"; String username = "用户名"; String password = "密码"; String remoteFilePath = "远程文件路径"; String localFilePath = "本地保存路径"; JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(username, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.get(remoteFilePath, localFilePath); channelSftp.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } finally { if (session != null) { session.disconnect(); } } } } ``` 请将代码的以下变量替换为实际的值: - `host`: 远程服务器的IP地址或域名 - `username`: 远程服务器的用户名 - `password`: 远程服务器的密码 - `remoteFilePath`: 要下载文件在远程服务器上的路径 - `localFilePath`: 下载后保存到本地的路径 这段代码使用jsch库创建一个SSH会话并连接到远程服务器。然后,通过打开SFTP通道并调用`get()`方法来下载文件。最后,关闭SFTP通道和SSH会话。 请注意,为了运行这段代码,你需要将jsch库添加到你的项目。你可以从jsch官方网站[https://www.jcraft.com/jsch/](https://www.jcraft.com/jsch/)下载该库的jar文件,并将其添加到你的项目的类路径
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值