技术标签: java sftp springboot
开发中经常需要从客户或者本公司的FTP服务器上下载、同步数据,那么FTP相关的知识就是必不可少的。
一般来说springboot使用ftp有两种,一是ftpClient,一种是sftp。由于ftpClient相比于sftp工具较为落后,对ssh支持度不高,故此选取sftp
一、pom
<!-- sftp -->
<dependency>
<groupId>jsch</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
二、配置
其实没有什么特别复杂的配置,使用的时候登陆就好,工具类如下:
package usi.dbdp.getTeleComConfigFile.util;
import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.Vector;
/**
* FTP服务器工具类:
* JSch类 通过 SFTP 协议上传文件到 freeSSHd 服务器
*
* @author xu
* @date 2020-7-16
*/
public class SftpUtil {
private Logger logger = LogManager.getLogger(SftpUtil.class);
private ChannelSftp sftp;
private Session session;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 秘钥
*/
private String privateKey;
/**
* FTP服务器Ip
*/
private String host;
/**
* FTP服务器端口号
*/
private int port;
/**
* 构造器:基于密码认证sftp对象
* @param username 用户名
* @param password 密码
* @param host 服务器ip
* @param port 服务器端口号
*/
public SftpUtil(String username, String password, String host, int port){
this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
/**
* 连接SFTP服务器
*/
public void login(){
JSch jsch = new JSch();
try{
if(privateKey != null){
//设置登陆主机的秘钥
jsch.addIdentity(privateKey);
}
//采用指定的端口连接服务器
session = jsch.getSession(username,host,port);
if(password != null){
//设置登陆主机的密码
session.setPassword(password);
}
//优先使用 password 验证 注:session.connect()性能低,使用password验证可跳过gssapi认证,提升连接服务器速度
session.setConfig("PreferredAuthentications","password");
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking","no");
session.connect();
//创建sftp通信通道
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
logger.info("sftp server connect success !!");
}catch (JSchException e){
logger.error("SFTP服务器连接异常!!", e);
}
}
/**
* 关闭SFTP连接
*/
public void logout(){
if(sftp != null){
if(sftp.isConnected()){
sftp.disconnect();
logger.info("sftp is close already");
}
}
if(session != null){
if(session.isConnected()){
session.disconnect();
logger.info("session is close already");
}
}
}
/**
* 将输入流上传到SFTP服务器,作为文件
*
* @param directory 上传到SFTP服务器的路径
* @param sftpFileName 上传到SFTP服务器后的文件名
* @param input 输入流
* @throws SftpException
*/
public void upload(String directory, String sftpFileName, InputStream input) throws SftpException{
long start = System.currentTimeMillis();
try{
//如果文件夹不存在,则创建文件夹
if(sftp.ls(directory) == null){
sftp.mkdir(directory);
}
//切换到指定文件夹
sftp.cd(directory);
}catch (SftpException e){
//创建不存在的文件夹,并切换到文件夹
sftp.mkdir(directory);
sftp.cd(directory);
}
sftp.put(input, sftpFileName);
logger.info("文件上传成功!! 耗时:{}ms",(System.currentTimeMillis() - start));
}
/**
* 上传单个文件
*
* @param directory 上传到SFTP服务器的路径
* @param uploadFileUrl 文件路径
*/
public void upload(String directory, String uploadFileUrl){
File file = new File(uploadFileUrl);
try{
upload(directory, file.getName(), new FileInputStream(file));
}catch (FileNotFoundException | SftpException e){
logger.error("上传文件异常!", e);
}
}
/**
* 将byte[] 上传到SFTP服务器,作为文件
* 注: 从String转换成byte[] 需要指定字符集
*
* @param directory 上传到SFTP服务器的路径
* @param sftpFileName 上传SFTP服务器后的文件名
* @param bytes 字节数组
*/
public void upload(String directory, String sftpFileName, byte[] bytes){
try{
upload(directory, sftpFileName, new ByteArrayInputStream(bytes));
}catch (SftpException e){
logger.error("上传文件异常!", e);
}
}
/**
* 将字符串按照指定编码格式上传到SFTP服务器
*
* @param directory 上传到SFTP服务器的路径
* @param sftpFileName 上传SFTP服务器后的文件名
* @param dataStr 字符串
* @param charsetName 字符串的编码格式
*/
public void upload(String directory, String sftpFileName, String dataStr, String charsetName){
try{
upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
}catch (UnsupportedEncodingException | SftpException e){
logger.error("上传文件异常!", e);
}
}
/**
* 下载文件
*
* @param directory SFTP服务器的文件路径
* @param downloadFile SFTP服务器上的文件名
* @param saveFile 保存到本地路径
*/
public void download(String directory, String downloadFile, String saveFile){
try{
if(directory != null && !"".equals(directory)){
sftp.cd(directory);
}
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
}catch (SftpException | FileNotFoundException e){
logger.error("文件下载异常!", e);
}
}
/**
* 下载文件
*
* @param directory SFTP服务器的文件路径
* @param downloadFile SFTP服务器上的文件名
* @return 字节数组
*/
public byte[] download(String directory, String downloadFile){
try{
if(directory != null && !"".equals(directory)){
sftp.cd(directory);
}
InputStream inputStream = sftp.get(downloadFile);
return IOUtils.toByteArray(inputStream);
}catch (SftpException | IOException e){
logger.error("---------------------------文件下载异常!---------------------------", e);
}
return null;
}
/**
* 下载文件
*
* @param directory SFTP服务器的文件路径
* @param downloadFile SFTP服务器上的文件名
* @return 输入流
*/
public InputStream downloadStream(String directory, String downloadFile){
try{
if(directory != null && !"".equals(directory)){
sftp.cd(directory);
}
return sftp.get(downloadFile);
}catch (SftpException e){
logger.error("文件下载异常!", e);
}
return null;
}
/**
* 删除文件
*
* @param directory SFTP服务器的文件路径
* @param deleteFileName 删除的文件名称
*/
public void delete(String directory, String deleteFileName){
try{
sftp.cd(directory);
sftp.rm(deleteFileName);
}catch (SftpException e){
logger.error("文件删除异常!", e);
}
}
/**
* 删除文件夹
*
* @param directory SFTP服务器的文件路径
*/
public void delete(String directory){
Vector vector = listFiles(directory);
vector.remove(0);
vector.remove(0);
for(Object v : vector){
ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry)v;
try{
sftp.cd(directory);
sftp.rm(lsEntry.getFilename());
}catch (SftpException e){
logger.error("文件删除异常!", e);
}
}
}
/**
* 获取文件夹下的文件
*
* @param directory 路径
* @return
*/
public Vector<?> listFiles(String directory){
try{
if(isDirExist(directory)){
Vector<?> vector = sftp.ls(directory);
//移除上级目录和根目录:"." ".."
vector.remove(0);
vector.remove(0);
return vector;
}
}catch (SftpException e){
logger.error("获取文件夹信息异常!", e);
}
return null;
}
/**
* 检测文件夹是否存在
*
* @param directory 路径
* @return
*/
public boolean booleanUrl(String directory){
try{
if(sftp.ls(directory) == null){
return false;
}
}catch (Exception e){
logger.error("---------------------检测文件夹异常!--------------------", e);
}
return true;
}
/**
* 创建一个文件目录
*
* @param createpath 路径
* @return
*/
public boolean createDir(String createpath) {
try {
if (isDirExist(createpath)) {
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString())) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
this.sftp.cd(createpath);
} catch (SftpException e) {
logger.error("目录创建异常!", e);
return false;
}
return true;
}
/**
* 判断目录是否存在
* @param directory 路径
* @return
*/
public boolean isDirExist(String directory) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = this.sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
/**
* 方法功能说明:目录不存在时创建目录
* @参数: @param path
* @return void
* @throws
*/
public void mkdirs(String path){
File file = new File(path);
String fs = file.getParent();
file = new File(fs);
if(!file.exists()){
file.mkdirs();
}
}
/**
* 判断文件或目录是否存在
* @param directory 路径
* @return
*/
public boolean isExist(String path,ChannelSftp sftp){
boolean isExist=false;
try {
sftp.lstat(path);
isExist = true;
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isExist = false;
}
}
return isExist;
}
}
三、具体使用
public String readFile(String remoteFileName,String remoteDir) {
SftpUtil sftpUtil = new SftpUtil(username, password, host, port);
sftpUtil.login();
String res = new String(sftpUtil.download(remoteDir, remoteFileName));;
if(StringUtils.isEmpty(res)) {
logger.info("----------------trying download again---------------");
}
if(StringUtils.isEmpty(res)) {
logger.error("--------------response file is null---------------");
}
return StringUtils.isEmpty(res)?null:res;
}
文章浏览阅读2.8k次,点赞3次,收藏7次。在MATLAB中,ones函数用于创建一个指定大小的由1组成的矩阵或数组。_matlab中ones函数
文章浏览阅读3.9w次,点赞2次,收藏9次。 在使用电脑办公过程中,安装应用程序时难免遇到无法安装或者无法正常启动的问题,这对我们使用电脑带来了诸多不便。那遇到应用程序无法正常启动的问题要如何解决呢?相信大家肯定都是十分疑问的,每次都是只能忍痛重新安装软件。今天,小编就和大家探讨下应用程序无法正常启动的解决方法,帮助大家排忧解难。0xc000007b电脑图解1 第一种方案:SFC检查系统完整性来尝试修复丢失文件 1、打开电脑搜索输入cmd.exe,选择以管理员身份运行,跳出提示框时选择继续。0xc000007b电脑图解2_photoshop应用程序无法正常启动0xc000007b。请单击“确认”关闭应用程序。
文章浏览阅读396次。1、概念 REDO LOG是Oracle为确保已经提交的事务不会丢失而建立的一个机制。实际上REDO LOG的存在是为两种场景准备的:实例恢复(INSTANCE RECOVERY);介质恢复(MEDIA RECOVERY)。 实例恢复的目的是在数据库发生故障时,确保BUFFER CACHE中的数据不会丢失,不会造成数据库的..._oracle 实例恢复和介质恢复
文章浏览阅读7k次。HashMap map1=new HashMap(); HashMap map2 = new HashMap();map2.putAll(map1)上面的代码的意思是将map1中所有的<key,value>插入到map2中去。具体参考: HashMap map1=new HashMap(); map1.put("..._java map putall
文章浏览阅读1w次,点赞8次,收藏60次。网上购物商城系统以弥补传统购物方式的弊端。在目前的商城里,如果采用网上商城方式,用户购物时就不需要到店里面排队,这样不仅能实时地了解商品的特色,而且方便了顾客,同时也减轻了商城的服务压力。随着WLAN技术的普及,一些规模较大的商城开始考虑利用这种最为先进、最具时尚潮流的网络技术来建设一套灵活、快捷的信息管理系统,以优化管理流程,使网上的服务和管理得到实质性的提升,提高品牌形象和行业核心竞争力。 本课题是实现了网上购物系统的功能,如:商品分类、订单管理等功能。本设计采用安装 JAVA JDK 、安装 IDEA_基于java web的网店管理系统的设计与实现
文章浏览阅读10w+次,点赞9k次,收藏2.4w次。关于Python爬虫的超详细讲解,用例子来给大家一步步分析爬虫的代码原理,由浅入深,老年人来了,我也给你整明白。_python爬取数据
文章浏览阅读134次。select 5000/10000.0 --想变成0.5select 5500/10000.0 --想变成0.55select 5550/10000.0 --想变成0.555select 5555/10000.0 --想变成0.5555其结果分别为:0.5000000 0.5500000 0.5550000 0.5555000一、如果想去掉数字5后面多余的0 ,需要转化一下:selec..._sql server 去小数 0
文章浏览阅读3.1k次。例一:import { Injectable } from '@angular/core';import { Observable } from 'rxjs';import { User } from "./model/User";import { map } from 'rxjs/operators';import { Http, Response, Headers, RequestOp..._angular6,requestoptions改成了什么
文章浏览阅读7.9k次,点赞17次,收藏110次。C++读取XML文件_c++ xml解析
文章浏览阅读235次。R语言使用caret包的train函数构建多元自适应回归样条(MARS)模型构建分类模型、trainControl函数设置交叉验证参数、自定义调优评估指标、tuneLength参数和tuneGrid参数超参数调优_多元自适应回归样条 r
文章浏览阅读5.8k次。Android中ListView是比较常用的控件,但一直都觉得创建ListView步骤有点繁琐,故在此总结一下,方便查阅。程序效果是实现一个显示联系人的简单信息。使用ListView控件,数据有姓名,和照片,并加入点击事件响应。布局文件:
文章浏览阅读535次。《2016最新二级C语言考试题库及答案(程序改错专项练习 精华版)》由会员分享,可在线阅读,更多相关《2016最新二级C语言考试题库及答案(程序改错专项练习 精华版)(4页珍藏版)》请在人人文库网上搜索。1、程序改错题(共15题)1、在考生文件夹下,给定程序MODI.C的功能是:求一维数组a中所有元素的平均值,结果保留两位小数。例如,当一维数组a中的元素为:10,4,2,7,3,12,5,34,5..._安徽二级python考试有程序改错题吗?