微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java实现sftp客户端上传文件以及文件夹的功能代码

本篇文章主要介绍了java实现sftp客户端上传文件以及文件夹的功能代码,具有一定的参考价值,有兴趣的可以了解一下。

1.依赖的jar文件 jsch-0.1.53.jar

2.登录方式有密码登录,和密匙登录

 代码:

函数:

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

文件上传代码:

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 exception e.printstacktrace(); } } catch (Exception e) { // Todo Auto-generated catch block e.printstacktrace(); } for (int i = 0; i

读取配置文件代码:

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 block e.printstacktrace(); } Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e) { // Todo Auto-generated catch block e.printstacktrace(); } return properties; } }

代码目录结构:

测试运行时配置文件放在项目的bin目录下(打包成可运行jar文件的时候要删除,打包完成后将配置文件jar包放在同级目录下即可):

properties.properties

ip= user= pwd= port=22 privateKeyPath= passphrase= sourcePath= destinationPath=/home/dbbs/f

打包可运行jar文件:

Export->java->Runnabe JAR file

完成后

在控制台运行java -jar  导出jar包的名字.jar 即可

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐