java使用Jsch组件链接资源上传下载文件

10 篇文章 0 订阅

java使用Jsch组件链接资源上传下载文件

依赖:jsch-0.1.55.jar

package com.zxl.demo.sftp;

import com.jcraft.jsch.*;
import org.springframework.stereotype.Component;

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

/**
 * sftp操作类
 * @author zml
 */
@Component
public class SftpClientUtil {

	public static Session session = null;
	public static Channel channel = null;
	private  static JSch jsch;
	private  static ChannelSftp sftp;

	public static void main(String[] args) {
		sftp = jschConnect("test","test","127.0.0.1",22);
		// 上传文件
		putFileEditDir("D:\\liang\\电子合同.pdf","/home/liang/files/电子合同.pdf");
		// 下载文件且删除服务器文件
		getFileToFolder("D:\\liang\\电子合同1.pdf","/home/liang/files/电子合同.pdf");
	}

	private static ChannelSftp jschConnect(String userName,String pwd, String IP,int port) {
		try {
			jsch = new JSch();
			session = jsch.getSession(userName, IP);
			session.setPassword(pwd);
			session.setPort(port);
			Properties sshConfig = new Properties();
			// 不校验秘钥
			sshConfig.put("StrictHostKeyChecking", "no");
			session.setConfig(sshConfig);
			// 超时时间
			session.setTimeout(3000);
			session.connect();
			channel = session.openChannel("sftp");
			channel.connect();
			System.out.println("链接成功");
		} catch (JSchException e) {
			if (channel != null) {
				channel.disconnect();
			}
			if (session != null) {
				session.disconnect();
			}
			System.out.println("sftp链接异常:" + e.getMessage());
		}
		return (ChannelSftp) channel;
	}

	public static void closeChannel() throws Exception {
		if (channel != null) {
			channel.disconnect();
		}
		if (session != null) {
			session.disconnect();
		}
	}

	// 上传文件至sftp服务器(编辑)
	public static boolean putFileEditDir(String localPath, String sftpPath) {
		boolean result = false;
		try {
			System.out.println("=====putFileEditDir=====start");
			System.out.println("文件保存路径:" + sftpPath);
			if (!lstat(sftp,sftpPath)){
				sftp.put(new FileInputStream(new File(localPath)), sftpPath);
				closeChannel();
				System.out.println("上传文件成功");
			}
			System.out.println("=====putFileEditDir=====end");
			result = true;
		} catch (Exception e) {
			System.out.println("sftp上传文件异常:{}"+e.getMessage());
			return result;
		}
		return result;
	}

	// 从sftp服务器下载文件至网盘
	public static boolean getFileToFolder(String localPath, String sftpPath) {
		System.out.println("=====getFileToFolder=====start");
		boolean result = false;
		try {
			if (lstat(sftp,sftpPath)){
				System.out.println("sftp服务文件路径:" + sftpPath);
				System.out.println("网盘文件保存路径:" + localPath);
				sftp.get(sftpPath, new FileOutputStream(new File(localPath)));
				System.out.println("下载成功!删除sftp服务器文件{}:" + sftpPath);
				sftp.rm(sftpPath);
			}
			closeChannel();
			result = true;
		} catch (Exception e) {
			System.out.println("sftp下载文件异常或文件不存在:{}"+ e.getMessage());
			return false;
		}
		System.out.println("=====getFileToFolder=====end");
		return result;
	}

	// 查看sftp服务器文件是否存在
	private static boolean lstat(ChannelSftp sftp, String sftpFilePath){
		try {
			SftpATTRS lstat = sftp.lstat(sftpFilePath);
			if (lstat == null){
				System.out.println("{}文件不存在!不做处理"+sftpFilePath);
				return false;
			}
			System.out.println("{}文件存在!"+sftpFilePath);
		} catch (SftpException e) {
			System.out.println("{}文件不存在!不做处理"+sftpFilePath);
			return false;
		}
		return true;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值