SFTPUtils.java

import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector; @Component
@Slf4j
public class SFTPUtils {
private SFTPUtils instance = null;
private ChannelSftp sftp; public File downloadFtpFile(String ftpHost,String ftpUserName,String ftpPassword,int ftpPort,String ftpPath,String localPath, String fileName, String downName){
if(null==instance){
instance=new SFTPUtils();
}
//获取SFTP连接
sftp = instance.connect(ftpHost, ftpPort, ftpUserName,ftpPassword);
//SFTP上下载文件
return instance.download(ftpPath+fileName, localPath+downName);
}
/**
* 连接sftp服务器
*
* @param host 主机
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
*/
public ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
log.info("SFTP Session connected.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("Connected to " + host);
} catch (Exception e) {
log.error(e.getMessage());
}
return sftp;
} /**
* 上传文件
*
* @param directory 上传的目录
* @param uploadFile 要上传的文件
*/
public boolean upload(String directory, String uploadFile) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
FileInputStream fileInputStream = new FileInputStream(file);
sftp.put(fileInputStream, file.getName());
fileInputStream.close();
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
} /**
* 下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
*/
public File download(String directory, String downloadFile, String saveFile) {
try {
sftp.cd(directory);
File file = new File(saveFile);
FileOutputStream fileOutputStream = new FileOutputStream(file);
sftp.get(downloadFile, fileOutputStream);
fileOutputStream.close();
return file;
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
} /**
* 下载文件
*
* @param downloadFilePath 下载的文件完整目录
* @param saveFile 存在本地的路径
*/
public File download(String downloadFilePath, String saveFile) {
try {
int i = downloadFilePath.lastIndexOf('/');
if (i == -1)
return null;
sftp.cd(downloadFilePath.substring(0, i));
File file = new File(saveFile);
FileOutputStream fileOutputStream = new FileOutputStream(file);
sftp.get(downloadFilePath.substring(i + 1), fileOutputStream);
fileOutputStream.close();
return file;
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
} /**
* 删除文件
*
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
*/
public void delete(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
log.error(e.getMessage());
}
} /**
* 断开连接
*/
public void disconnect() {
try {
sftp.getSession().disconnect();
} catch (JSchException e) {
log.error(e.getMessage());
}
sftp.quit();
sftp.disconnect();
} /**
* 列出目录下的文件
*
* @param directory 要列出的目录
* @throws SftpException
*/
@SuppressWarnings("unused")
public Vector<LsEntry> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
}

SFTP 文件上传下载工具类的更多相关文章

  1. Spring MVC文件上传下载工具类

    import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...

  2. ftp上传下载工具类

    package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...

  3. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  4. 我的代码库-Java8实现FTP与SFTP文件上传下载

    有网上的代码,也有自己的理解,代码备份 一般连接windows服务器使用FTP,连接linux服务器使用SFTP.linux都是通过SFTP上传文件,不需要额外安装,非要使用FTP的话,还得安装FTP ...

  5. Java实现FTP与SFTP文件上传下载

    添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...

  6. Jsch - java SFTP 文件上传下载

    使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...

  7. Python Paramiko实现sftp文件上传下载以及远程执行命令

    一.简介 Paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 安装模块 默认Python没有自带,需要手动安装: pip3 install par ...

  8. java实现sftp文件上传下载

    /** * * @param filePath 文件全路径 * @param ftpPath 上传到目的端目录 * @param username * @param password * @param ...

  9. SFTP 文件上传下载引用代码

    http://sha1064616837.iteye.com/blog/2036996 http://www.cnblogs.com/itmanxgl/p/fe5d33512609fe540eb08a ...

随机推荐

  1. LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted

    公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...

  2. 出师表(ENGLISH) 强烈打call啊~王洛勇是什么神仙英语

    臣亮言:先帝创业未半而中道崩殂, Permit me to observe: the late emperor was taken from us before he could finish his ...

  3. SpringCloud入门概述

    SpringCloud入门概述 Spring的三大模块:SpringBoot(构建),Spring Cloud(协调),Spring Cloud Data Flow(连接)注意:Spring Boot ...

  4. 【More Effective C++ 条款4】非必要不提供default constructor

    1)default constructor:在没有任何外来信息的情况下将对象初始化 2)但是有些对象如果没有外来信息,就没有办法完成初始化动作,那么这些对象,就没有必要提供default constr ...

  5. CAS5单点登录

    看这篇文章即可:https://www.jianshu.com/p/c1273d81c4e4>https://www.jianshu.com/p/c1273d81c4e4

  6. 获取PostgreSQL数据库中得JSON值

    在PostgreSQL数据库中有一列为JSON,要获取JSON中得数据可以用下面sql: select orderno as OrderNo ,amount as Amount ,ordertime ...

  7. wcf序列化嵌套类(如TreeNode)异常原因

    循环引用类在WCF中的传递 循环引用类在WCF中的传递问题,例如: [DataContract]    public class AB    {        public string name { ...

  8. 二、NodeJS入门——准备工作(2)——MongoDB安装以及客户端Robomongo安装和使用

    目录     1.介绍     2.下载地址     3.MongoDB安装过程     4.MongoDB的使用     5.MongoDB添加管理员账户     6.RoboMongo安装过程   ...

  9. 看一下“Dubbo 2.7”的三大新特性

    Dubbo 2.7.x 作为 Apache 的孵化版本,除了代码优化之外,还新增了许多重磅的新特性,本文将会介绍其中最典型的三个新特性: 一.异步化改造 二.三大中心改造 三.服务治理增强 一.异步支 ...

  10. navicat2059错误的解决

    1.输入mysql -uroot -p登陆mysql 2.登录成功以后使用ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD ...