当前位置: 首页 > news >正文

许昌市做网站汉狮网络百度广告代理商加盟

许昌市做网站汉狮网络,百度广告代理商加盟,建设集团企业网站,河北沧州疫情最新数据Java之通过JSch库连接Linux实现文件传输 文章目录 Java之通过JSch库连接Linux实现文件传输1. JSch2. Java通过Jsch连接Linux1. poxm.xml2. 工具类3. 调用案例 1. JSch 官网:JSch - Java Secure Channel (jcraft.com) JSch是SSH2的纯Java实现。 JSch 允许您连接到 ss…

Java之通过JSch库连接Linux实现文件传输

文章目录

  • Java之通过JSch库连接Linux实现文件传输
  • 1. JSch
  • 2. Java通过Jsch连接Linux
    • 1. poxm.xml
    • 2. 工具类
    • 3. 调用案例

1. JSch

官网:JSch - Java Secure Channel (jcraft.com)

JSch是SSH2的纯Java实现。
JSch 允许您连接到 sshd 服务器并使用端口 转发、X11 转发、文件传输等,以及 您可以将其功能集成到您自己的 Java 程序中。 JSch在BSD风格的许可证下获得许可。

2. Java通过Jsch连接Linux

1. poxm.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yuan</groupId><artifactId>yuan-connect-linux</artifactId><version>1.0.0</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version></dependency><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version></dependency></dependencies>
</project>

2. 工具类

SftpUtils.java如下:

package com.yuan.utils;import com.jcraft.jsch.*;
import org.apache.commons.lang3.StringUtils;import java.io.*;
import java.util.*;
import java.util.logging.Logger;//在使用的的时候,需要调用 connect()开启连接,使用完后调用 disconnect() 关闭连接 。
public class SftpUtils {Logger log = Logger.getLogger(SftpUtils.class.getName());/*** 主机名称/IP*/private String host;/*** 端口,默认22*/private int port = 22;/*** 用户名*/private String userName;/*** 密码*/private String password;private ChannelSftp sftp = null;private Channel channel = null;private Session session = null;public SftpUtils(String host, String userName, String password) {this.host = host;this.userName = userName;this.password = password;}public SftpUtils(String host, int port, String userName, String password) {this.host = host;this.port = port;this.userName = userName;this.password = password;}/*** 连接到服务器** @throws JSchException*/public void connect() throws JSchException {JSch jSch = new JSch();session = jSch.getSession(userName, host, port);session.setPassword(password);session.setConfig(this.config());session.connect();channel = session.openChannel("sftp");channel.connect();sftp = (ChannelSftp) channel;log.info("以连接到主机" + host);}/*** config** @return Properties*/private Properties config() {Properties properties = new Properties();properties.put("StrictHostKeyChecking", "no");return properties;}/*** 关闭连接*/public void closeConnect() {try {if (sftp.isConnected()) {sftp.disconnect();}if (channel.isConnected()) {channel.disconnect();}if (session.isConnected()) {session.disconnect();}} catch (Throwable e) {e.printStackTrace();}}/*** 下载文件** @param remoteDir      远程服务器文件目录,不指定路径默认是FTP的根路径,指定路径是指的SFTP的根路径下开始。*                       例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;*                       指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。* @param fileName       文件名,如:myTest.pdf* @param outputFilePath 下载文件保存路径,目录+文件名,例如:d:/pdf/myTest.pdf* @return*/public boolean downloadFile(String remoteDir, String fileName, String outputFilePath) {OutputStream outputStream = null;try {if (StringUtils.isNotBlank(remoteDir)) {sftp.cd(remoteDir);}outputStream = new FileOutputStream(outputFilePath);sftp.get(fileName, outputStream);return true;} catch (Exception e) {log.info("下载文件错误," + e.getMessage());} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/*** 文件上传** @param remotePath    远程服务器目录,不指定路径默认是FTP的根目录,指定路径是指的SFTP的根目录下开始。*                      例如:SFTP根路径为:/opt/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;*                      指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。* @param fileName      上传后文件名* @param localFilePath 本地文件路径,目录+文件名,例如:d:/aa/myTest.pdf* @return*/public boolean uploadFile(String remotePath, String fileName, String localFilePath) {InputStream inputStream = null;try {if (StringUtils.isNotBlank(remotePath)) {sftp.cd(remotePath);}inputStream = new FileInputStream(localFilePath);sftp.put(inputStream, fileName);return true;} catch (Exception e) {log.info("文件上传失败:" + e.getMessage());} finally {if (null != inputStream) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/*** 上传文件** @param remotePath  远程服务器目录,不指定路径默认是FTP的根目录,指定路径是指的SFTP的根目录下开始。*                    例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;*                    指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。* @param fileName    上传后文件名* @param inputStream 文件输入流* @return*/public boolean uploadFile(String remotePath, String fileName, InputStream inputStream) {try {if (StringUtils.isNotBlank(remotePath)) {sftp.cd(remotePath);}sftp.put(inputStream, fileName);return true;} catch (Exception e) {log.info("文件上传失败:" + e.getMessage());} finally {if (null != inputStream) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/*** 文件删除** @param remotePath 远程服务器目录,不指定路径默认是FTP的根目录,指定路径是指的SFTP的根目录下开始。*                   例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;*                   指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。* @param fileName   文件名* @return*/public boolean deleteFile(String remotePath, String fileName) {try {if (StringUtils.isNotBlank(remotePath)) {sftp.cd(remotePath);}sftp.rm(fileName);return true;} catch (Exception e) {log.info("删除文件失败:" + e.getMessage());}return false;}/*** 查询指定目录下所有文件** @param remotePath 远程服务器目录,不指定路径默认是FTP的根目录,指定路径是指的SFTP的根目录下开始。*                   例如:SFTP根路径为:/sftp/file,那么默认下载文件会去根路径下载,而指定 sftpPath 也是从根路径下开始;*                   指定 sftpPath 为 word,那么是从 /sftp/file/word 路径中查找文件下载。为空表示忽略该参数。* @return*/public List<String> listFiles(String remotePath) throws SftpException {Vector files = sftp.ls(remotePath);List<String> result = new ArrayList<>();Iterator iterator = files.iterator();while (iterator.hasNext()) {ChannelSftp.LsEntry isEntity = (ChannelSftp.LsEntry) iterator.next();result.add(isEntity.getFilename());}return result;}
}

3. 调用案例

package com.yuan;import com.yuan.utils.SftpUtils;public class SftpDemo {public static void main(String[] args) {SftpUtils sftpUtils = null;try {sftpUtils = new SftpUtils("??", 22, "??", "??");sftpUtils.connect();sftpUtils.downloadFile("/opt/download/", "myTest.pdf", "D:\\aa\\pdf\\yuan.pdf");} catch (Exception e) {e.printStackTrace();} finally {if (sftpUtils != null) {sftpUtils.closeConnect();}}}
}
http://www.hrbkazy.com/news/19589.html

相关文章:

  • 中国城镇化建设委员会的网站凡科建站登录入口
  • 做网站挣钱来个好心人指点一下呗西安网站推广排名
  • 有了域名后怎么完成网站建设搜索关键词排名工具
  • 如何做网站路径分析sem代运营托管公司
  • 厦门入夏网站建设公司怎么查询最新网站
  • 一品威客官方网站网络营销咨询服务
  • 青海省网络公司大连网络营销seo
  • 贵州建设厅网站官网网站老域名跳转到新域名
  • 丰县网站建设汽车seo是什么意思
  • 重庆设计公司网站重庆seo关键词排名
  • 静安区社会建设办公室网站长安网站优化公司
  • 沈阳企业网站模板建站自己如何注册网站
  • 长春好的做网站公司有哪些nba湖人队最新消息
  • 做移动网站排名软件北京云无限优化
  • app是什么软件首页关键词排名优化
  • 17一起做网站zwd.com关键词优化师
  • 阿里云虚拟主机网站建设免费宣传平台有哪些
  • 如何做推广链接搜狗seo刷排名软件
  • html的网站模板浙江seo推广
  • 专门做油画交流的网站百度一下你就知道了主页
  • 网站如何做才能被360收录百度一下官方网
  • 甘肃兰州天气预报15天网站优化建议
  • 深圳网站建设公司哪个搜索引擎优化的重要性
  • 网站经常被挂马网站快速优化排名app
  • html网站标签百度手机助手应用商店
  • 企业英文网站制作网站优化排名易下拉霸屏
  • 微商货源类网站源码视频号链接怎么获取
  • 平面设计赚钱网站投广告的平台有哪些
  • 建平台网站软文营销软文推广
  • 帮别人做网站 别人违法app引流推广软件