java实现sftp客户端上传⽂件以及⽂件夹的功能代码1.依赖的jar⽂件 jsch-0.1.53.jar2.登录⽅式有密码登录,和密匙登录代码:主函数:import java.util.Properties;import com.cloudpower.util.Login;import com.util.LoadProperties;public class Ftp {public static void main(String[] args) {Properties properties = LoadProperties.getProperties();Login.login(properties);}}登陆页⾯的代码:package com.cloudpower.util;import java.io.Console;import java.util.Properties;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;public class Login {public static void login(Properties properties) {String ip = properties.getProperty("ip");String user = properties.getProperty("user");String pwd = properties.getProperty("pwd");String port = properties.getProperty("port");String privateKeyPath = properties.getProperty("privateKeyPath");String passphrase = properties.getProperty("passphrase");String sourcePath = properties.getProperty("sourcePath");String destinationPath = properties.getProperty("destinationPath");if (ip != null && !ip.equals("") && user != null && !user.equals("")&& port != null && !port.equals("") && sourcePath != null&& !sourcePath.equals("") && destinationPath != null&& !destinationPath.equals("")) {if (privateKeyPath != null && !privateKeyPath.equals("")) {sshSftp2(ip, user, Integer.parseInt(port), privateKeyPath,passphrase, sourcePath, destinationPath);} else if (pwd != null && !pwd.equals("")) {sshSftp(ip, user, pwd, Integer.parseInt(port), sourcePath,destinationPath);} else {Console console = System.console();System.out.print("Enter password:");char[] readPassword = console.readPassword();sshSftp(ip, user, new String(readPassword),Integer.parseInt(port), sourcePath, destinationPath);}} else {System.out.println("请先设置配置⽂件");}}/*** 密码⽅式登录** @param ip* @param user* @param psw* @param port* @param sPath* @param dPath*/public static void sshSftp(String ip, String user, String psw, int port,String sPath, String dPath) {System.out.println("password login");Session session = null;JSch jsch = new JSch();try {if (port <= 0) {// 连接服务器,采⽤默认端⼝session = jsch.getSession(user, ip);} else {// 采⽤指定的端⼝连接服务器session = jsch.getSession(user, ip, port);}// 如果服务器连接不上,则抛出异常if (session == null) {throw new Exception("session is null");}// 设置登陆主机的密码session.setPassword(psw);// 设置密码// 设置第⼀次登陆的时候提⽰,可选值:(ask | yes | no)session.setConfig("StrictHostKeyChecking", "no");// 设置登陆超时时间session.connect(300000);UpLoadFile.upLoadFile(session, sPath, dPath);} catch (Exception e) {e.printStackTrace();}System.out.println("success");}/*** 密匙⽅式登录** @param ip* @param user* @param port* @param privateKey* @param passphrase* @param sPath* @param dPath*/public static void sshSftp2(String ip, String user, int port,String privateKey, String passphrase, String sPath, String dPath) {System.out.println("privateKey login");Session session = null;JSch jsch = new JSch();try {// 设置密钥和密码// ⽀持密钥的⽅式登陆,只需在jsch.getSession之前设置⼀下密钥的相关信息就可以了 if (privateKey != null && !"".equals(privateKey)) {if (passphrase != null && "".equals(passphrase)) {// 设置带⼝令的密钥jsch.addIdentity(privateKey, passphrase);} else {// 设置不带⼝令的密钥jsch.addIdentity(privateKey);}}if (port <= 0) {// 连接服务器,采⽤默认端⼝session = jsch.getSession(user, ip);} else {// 采⽤指定的端⼝连接服务器session = jsch.getSession(user, ip, port);}// 如果服务器连接不上,则抛出异常if (session == null) {throw new Exception("session is null");}// 设置第⼀次登陆的时候提⽰,可选值:(ask | yes | no)session.setConfig("StrictHostKeyChecking", "no");// 设置登陆超时时间session.connect(300000);UpLoadFile.upLoadFile(session, sPath, dPath);System.out.println("success");} catch (Exception e) {e.printStackTrace();}}}⽂件上传的代码:package com.cloudpower.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Scanner;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.Session;import com.jcraft.jsch.SftpException;public class UpLoadFile {public static void upLoadFile(Session session, String sPath, String dPath) {Channel channel = null;try {channel = (Channel) session.openChannel("sftp");channel.connect(10000000);ChannelSftp sftp = (ChannelSftp) channel;try {sftp.cd(dPath);Scanner scanner = new Scanner(System.in);System.out.println(dPath + ":此⽬录已存在,⽂件可能会被覆盖!是否继续y/n?"); String next = scanner.next();if (!next.toLowerCase().equals("y")) {return;}} catch (SftpException e) {sftp.mkdir(dPath);sftp.cd(dPath);}File file = new File(sPath);copyFile(sftp, file, sftp.pwd());} catch (Exception e) {e.printStackTrace();} finally {session.disconnect();channel.disconnect();}}public static void copyFile(ChannelSftp sftp, File file, String pwd) {if (file.isDirectory()) {File[] list = file.listFiles();try {try {String fileName = file.getName();sftp.cd(pwd);System.out.println("正在创建⽬录:" + sftp.pwd() + "/" + fileName);sftp.mkdir(fileName);System.out.println("⽬录创建成功:" + sftp.pwd() + "/" + fileName);} catch (Exception e) {// TODO: handle exception}pwd = pwd + "/" + file.getName();try {sftp.cd(file.getName());} catch (SftpException e) {// TODO: handle exceptione.printStackTrace();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}for (int i = 0; i < list.length; i++) {copyFile(sftp, list[i], pwd);}} else {try {sftp.cd(pwd);} catch (SftpException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}System.out.println("正在复制⽂件:" + file.getAbsolutePath()); InputStream instream = null;OutputStream outstream = null;try {outstream = sftp.put(file.getName());instream = new FileInputStream(file);byte b[] = new byte[1024];int n;try {while ((n = instream.read(b)) != -1) {outstream.write(b, 0, n);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (SftpException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {outstream.flush();outstream.close();instream.close();} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}}}读取配置⽂件的代码:package com.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class LoadProperties {public static Properties getProperties() {File file = new File(Class.class.getClass().getResource("/").getPath()+ "properties.properties");InputStream inputStream = null;try {inputStream = new FileInputStream(file);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Properties properties = new Properties();try {properties.load(inputStream);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return properties;}}代码⽬录结构:测试运⾏时配置⽂件放在项⽬的bin⽬录下(打包成可运⾏jar⽂件的时候要删除,打包完成后将配置⽂件和jar包放在同级⽬录下即可):properties.propertiesip=user=pwd=port=22privateKeyPath=passphrase=sourcePath=destinationPath=/home/dbbs/f打包可运⾏jar⽂件:Export->java->Runnabe JAR file完成后在控制台运⾏java -jar 导出jar包的名字.jar 即可以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。