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. mysql,字符串类型id,获取最大值

    说明,这个id是字符串类型,但是实际值是一个整数,获取最大值的方法是: select max(cast(id as SIGNED)) from table 另外,mysql生成伪列的方法: SELEC ...

  2. Windows下使用grep命令

    一.可供选择的工具列表: Grep for Windows – 轻量级选项 GNU utilities for Win32 – 本地港口 Cash – 重量轻,建于Node.js之上 Cygwin – ...

  3. Linq 将两个查询结果合称为一个

    var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...

  4. JS 对象属性名排序

    问题,对象属性名排序,如: var data = { A:[], D:[], B:{} } 调整为=> var data = { A:[], B:[], D:{} } 方法一: for,in,把 ...

  5. swagger 集成后发布到服务器报错[Could not find file 'D:\\home\\site\\wwwroot\\bin\\WebAPI.XML]

    webapi集成swagger后,在本地运行没有问题,但是发布到服务器上就有问题. 报错信息:Could not find file 'D:\\home\\site\\wwwroot\\bin\\We ...

  6. disconf的简单使用与远程配置更改为使用本地配置

    这几天因为阿里云迁移到腾讯云的原因,原来服务器上的disconf不再使用了.在这段时间里,系统出现的bug很难寻找原因(项目起不来),现在想要把disconf远程配置更改成直接使用本地配置.首先,了解 ...

  7. tkinter的单选Radiobutto

    from tkinter import * def printSelection(): num = var.get() if num == 1: lab.config(text="你是男生& ...

  8. 递归---Day29

    递归的概述 递归:指在当前方法内自己调用自己的方式叫做递归 递归的分类: 1.直接递归称为方法自身调用自己. 2.间接递归可以用A方法调用B方法,用B方法调用C方法,用C方法调用A方法. 递归的注意事 ...

  9. Qt json使用

    JSON 6 种基本数据类型 QJsonValue::Bool QJsonValue::Double QJsonValue::String QJsonValue::Array QJsonValue:: ...

  10. Java中的equals与==

    package demo; public class Test { public static void main(String[] args) { String str1 = new String( ...