linus jsch文件下载
package com.osplat.util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Vector;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.SystemUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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;
/** * linus文件下载 * @author Administrator * @date */public class SftpManager { private static final Logger logger = LoggerFactory.getLogger(SftpManager.class); public static final String SFTP_PROTOCAL = "sftp";
public static void main(String[] args) throws Exception { ChannelSftp sftp=null; sftp=connect("111.111.1.11", "root", "123456", 22); List<String> listFiles=listFiles(sftp, "/home/numa/image", null); downloadFileList(sftp, "/home/numa/image", "E:\\picture", listFiles); disconnect(sftp);
}
/** * * @param host * IP地址 * @param username * 用户名 * @param password * 密码 * @param port * 端口号 * @return * @throws Exception */ public static ChannelSftp connect(String host, String username, String password, int port) throws Exception { Channel channel = null; ChannelSftp sftp = null; JSch jsch = new JSch(); Session session = createSession(jsch, host, username, port); // 设置登陆主机的密码 session.setPassword(password); // 设置登陆超时时间 session.connect(15000); logger.info("Session connected to " + host + "."); try { // 创建sftp通信通道 channel = (Channel) session.openChannel(SFTP_PROTOCAL); channel.connect(1000); logger.info("Channel created to " + host + "."); sftp = (ChannelSftp) channel; } catch (JSchException e) { logger.error("exception when channel create.", e); } return sftp; }
public static void upload(ChannelSftp sftp, String srcFile, String dest) { try { File file = new File(srcFile); if (file.isDirectory()) { sftp.cd(srcFile); for (String fileName : file.list()) { sftp.put(srcFile + SystemUtils.FILE_SEPARATOR + fileName, dest); } } sftp.put(srcFile, dest); } catch (Exception e) { e.printStackTrace(); } }
public static void upload(ChannelSftp sftp, List<String> fileList, String destPath) throws SftpException { try { sftp.cd(destPath); } catch (Exception e) { sftp.mkdir(destPath); } for (String srcFile : fileList) { upload(sftp, srcFile, destPath); } }
/** * * @param * @param srcPath * 服务器上源文件的路径, 必须是目录 * @param saveFile * 下载后文件的存储路径, 若为目录, 则文件名将与目标服务器上的文件名相同 * @param srcfile * 目标服务器上的文件, 不能为目录 */ public static void download(ChannelSftp sftp, String srcPath, String saveFile, String srcfile) { try { sftp.cd(srcPath); File file = new File(saveFile); if (file.isDirectory()) { sftp.get(srcfile, new FileOutputStream(file + SystemUtils.FILE_SEPARATOR + srcfile)); } else { sftp.get(srcfile, new FileOutputStream(file)); } } catch (Exception e) { logger.error("download file: {} error", srcPath + SystemUtils.FILE_SEPARATOR + srcfile, e); } }
/** * 使用sftp下载目标服务器上某个目录下指定类型的文件, 得到的文件名与 sftp服务器上的相同 * * @param sftp * @param srcPath * sftp服务器上源目录的路径, 必须是目录 * @param savePath * 下载后文件存储的目录路径, 一定是目录, 如果不存在则自动创建 * @param fileTypes * 指定类型的文件, 文件的后缀名组成的字符串数组 */ public static void download(ChannelSftp sftp, String srcPath, String savePath, String... fileTypes) { List<String> fileList = new ArrayList<String>(); try { sftp.cd(srcPath); createDir(savePath); if (fileTypes.length == 0) { // 列出服务器目录下所有的文件列表 fileList = listFiles(sftp, srcPath, "*"); downloadFileList(sftp, srcPath, savePath, fileList); return; } for (String type : fileTypes) { fileList = listFiles(sftp, srcPath, "*" + type); parseAndUpdateDB(sftp, srcPath, savePath, fileList); } } catch (Exception e) { logger.error( "download all file in path = '" + srcPath + "' and type in " + Arrays.asList(fileTypes) + " error", e); } }
private static File createDir(String savePath) throws Exception { File localPath = new File(savePath); if (!localPath.exists() && !localPath.isFile()) { if (!localPath.mkdir()) { throw new Exception(localPath + " directory can not create."); } } return localPath; }
/** * sftp下载目标服务器上srcPath目录下所有指定的文件.<br/> * 若本地存储路径下存在与下载重名的文件,仍继续下载并覆盖该文件.<br/> * * @param sftp * @param srcPath * @param savePath * 文件下载到本地存储的路径,必须是目录 * @param fileList * 指定的要下载的文件名列表 * @throws SftpException * @throws FileNotFoundException */ public static void downloadFileList(ChannelSftp sftp, String srcPath, String savePath, List<String> fileList) throws SftpException, FileNotFoundException { sftp.cd(srcPath); for (String srcFile : fileList) { logger.info("srcFile: " + srcFile); String localPath = savePath + SystemUtils.FILE_SEPARATOR + srcFile; sftp.get(srcFile, localPath); } }
/** * sftp下载目标服务器上所有指定的文件, 并解析文件的内容.<br/> * 若本地存储路径下存在与下载重名的文件, 则忽略(不下载)该文件.<br/> * * @param sftp * @param srcPath * sftp上源文件的目录 * @param savePath * 文件下载到本地存储的路径,必须是目录 * @param fileList * 指定的要下载的文件列表 * @throws FileNotFoundException * @throws SftpException */ private static void parseAndUpdateDB(ChannelSftp sftp, String srcPath, String savePath, List<String> fileList) throws FileNotFoundException, SftpException { sftp.cd(srcPath); for (String srcFile : fileList) { String localPath = savePath + SystemUtils.FILE_SEPARATOR + srcFile; File localFile = new File(localPath); // savePath路径下已有文件与下载文件重名, 忽略这个文件 if (localFile.exists() && localFile.isFile()) { continue; } logger.info("start downloading file: [" + srcFile + "], parseAndUpdate to DB"); sftp.get(srcFile, localPath); // updateDB(localFile); } }
/** * 获取srcPath路径下以regex格式指定的文件列表 * * @param sftp * @param srcPath * sftp服务器上的目录 * @param regex * 需要匹配的文件名 * @return * @throws SftpException */ @SuppressWarnings("unchecked") public static List<String> listFiles(ChannelSftp sftp, String srcPath, String regex) throws SftpException { List<String> fileList = new ArrayList<String>(); sftp.cd(srcPath); // 如果srcPath不是目录则会抛出异常 if ("".equals(regex) || regex == null) { regex = "*"; } Vector<LsEntry> sftpFile = sftp.ls(regex); String fileName = null; for (LsEntry lsEntry : sftpFile) { fileName = lsEntry.getFilename(); fileList.add(fileName); } return fileList; }
/** * 删除文件 * * @param dirPath * 要删除文件所在目录 * @param file * 要删除的文件 * @param sftp * @throws SftpException */ public static void delete(String dirPath, String file, ChannelSftp sftp) throws SftpException { String now = sftp.pwd(); sftp.cd(dirPath); sftp.rm(file); sftp.cd(now); }
/** * Disconnect with server * * @param sftp */ public static void disconnect(ChannelSftp sftp) { try { if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } else if (sftp.isClosed()) { logger.info("sftp is closed already"); } if (null != sftp.getSession()) { sftp.getSession().disconnect(); } } } catch (JSchException e) { // Ignore } }
private static Session createSession(JSch jsch, String host, String username, int port) throws Exception { Session session = null; if (port <= 0) { // 连接服务器,采用默认端口 session = jsch.getSession(username, host); } else { // 采用指定的端口连接服务器 session = jsch.getSession(username, host, port); } // 如果服务器连接不上,则抛出异常 if (session == null) { throw new Exception(host + "session is null"); } // 设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); return session; }
/** * Private/public key authorization (加密秘钥方式登陆) * * @param username * 主机登陆用户名(user account) * @param host * 主机IP(server host) * @param port * 主机ssh登陆端口(ssh port), 如果port<=0, 取默认值22 * @param privateKey * 秘钥文件路径(the path of key file.) * @param passphrase * 密钥的密码(the password of key file.) * @return * @throws Exception */ public static ChannelSftp connect(String username, String host, int port, String privateKey, String passphrase) throws Exception { Channel channel = null; ChannelSftp sftp = null; JSch jsch = new JSch(); // 设置密钥和密码 ,支持密钥的方式登陆 if (StringUtils.isNotEmpty(privateKey)) { if (StringUtils.isNotEmpty(passphrase)) { // 设置带口令的密钥 jsch.addIdentity(privateKey, passphrase); } else { // 设置不带口令的密钥 jsch.addIdentity(privateKey); } } Session session = createSession(jsch, host, username, port); // 设置登陆超时时间 session.connect(15000); logger.info("Session connected to " + host + "."); try { // 创建sftp通信通道 channel = (Channel) session.openChannel(SFTP_PROTOCAL); channel.connect(1000); logger.info("Channel created to " + host + "."); sftp = (ChannelSftp) channel; } catch (JSchException e) { logger.error("exception when channel create.", e); } return sftp; }}
linus jsch文件下载的更多相关文章
- jsch文件下载功能
转载:http://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html 上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch ...
- linus jsch上传文件
package com.osplat.util; import java.io.*; import com.jcraft.jsch.*;import com.osplat.bean.Resultmod ...
- 【转】JSch - Java实现的SFTP(文件下载详解篇)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRI ...
- JSch - Java实现的SFTP(文件下载详解篇)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRITE ...
- JSch - Java实现的SFTP(文件下载详解篇)(转)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRI ...
- 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控
参考上篇文章: <[SFTP]使用Jsch实现Sftp文件下载-支持断点续传和进程监控>:http://www.cnblogs.com/ssslinppp/p/6248763.html ...
- 【SFTP】使用Jsch实现Sftp文件上传-支持断点续传和进程监控
JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...
- 【转】JSch - Java实现的SFTP(文件上传详解篇)
JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...
- JSch - Java实现的SFTP(文件上传详解篇)
JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...
随机推荐
- Java可视操作界面例子
package rom; import java.lang.*; import java.awt.*; import java.awt.event.ActionEvent; import java.a ...
- mongodb的管理员和安全认证
超级管理员 为了更安全的访问mongodb,需要访问者提供用户名和密码,于是需要在mongodb中创建用户 采用了角色-用户-数据库的安全管理方式 常用系统角色如下: root:只在admin数据库中 ...
- 寻找cost函数最小值:梯度下降与最小二乘法
Editted by MarkDown 寻找cost函数最小值:梯度下降与最小二乘法 参考:最小二乘法小结--刘建平 背景: 目标函数 = Σ(观测值-理论值)2 观测值就是我们的多组样本,理论值就是 ...
- 代码: html 页面小效果 (集合,待补充)
标签切换(下部内容区跟着切换): 2016-6-2 <script type="text/javascript" src="http://cdn.bootcss.c ...
- excel表格如何打斜杠
https://jingyan.baidu.com/article/5bbb5a1b3f471613eba17908.html
- 从一个异常探索spring autowired 的原理
从一个异常探索autowired 的原理. 首先环境是这样的: public class Boss { @Autowired private Car car; } //@Component 加上这个注 ...
- centos使用记
20180404:今天在笔记本上安装了centos6.9,第一次安装的7.4时进不了安装界面,后来下载了6.9版的,可以安装.安装完后启动时出现fail reg的错误,然后提示登陆,不知是用户名为:r ...
- 四、Java web 部 分试题
1 .Tomcat 的 优 化 经 验 答:去掉对 web.xml 的监视,把 jsp 提前编辑成 Servlet. 有富余物理内存的情况,加大 tomcat 使用的 jvm 的内存 2 .HTTP ...
- css样式表1
1内联样式表 和html联合使用,控制精确,但是可重用性差,冗余多. <p style="font-size:14px;"></p> <div sty ...
- 尚硅谷springboot学习22-Thymeleaf入门
Thymeleaf是一种模板引擎,类似于JSP.Velocity.Freemarker