This commit is contained in:
panbaolin 2026-01-12 15:41:40 +08:00
commit 61fdb7a0e8
3 changed files with 329 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package org.jeecg.baseAPI.sftpAPI;
/**
* @Description: 通用API
* @Author: jeecg-boot
* @Date: 2023-09-20
* @Version: V1.0
*/
public interface IBizSftpAPIService {
/**
* sftp上传文件
* @param file
* @param cshFilePath
* @param fileName
*/
void sftpUpload(String file, String cshFilePath, String fileName);
/**
* sftp下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
*/
void download(String directory, String downloadFile, String saveFile);
}

View File

@ -0,0 +1,245 @@
package org.jeecg.baseAPI.sftpAPI;
import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
public class SFTPUtil {
private ChannelSftp sftp;
private Session session;
@Value("${spring.Linux.ip}")
private String host;
@Value("${spring.Linux.username}")
private String username;
@Value("${spring.Linux.password}")
private String password;
@Value("${spring.Linux.port}")
private Integer port;
//私钥
private String privateKey;
// bizSftpAPIService.download(mcipPath,"GRIDDESC.d03",targetFilePath + "GRIDDESC.d03");
/**
* 构造基于密码认证的sftp对象
*/
// public SFTPUtil(String username, String password, String host, int port) {
// this.username = username;
// this.password = password;
// this.host = host;
// this.port = port;
// }
/**
* 构造基于秘钥认证的sftp对象
*/
// public SFTPUtil(String username, String host, int port, String privateKey) {
// this.username = username;
// this.host = host;
// this.port = port;
// this.privateKey = privateKey;
// }
public SFTPUtil() { }
/**
* 连接sftp服务器
*/
public void login() {
try {
JSch jsch = new JSch();
if (privateKey != null) {
jsch.addIdentity(privateKey);// 设置私钥
}
session = jsch.getSession(username, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60 * 1000 * 10);
System.out.println("session:"+session);
Channel channel = session.openChannel("sftp");
channel.connect(60 * 1000 * 10);
sftp = (ChannelSftp) channel;
System.out.println("login-sftp:" + sftp);
} catch (JSchException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 连接sftp服务器
*/
public void login(String username, String password, String host, Integer port) {
try {
JSch jsch = new JSch();
if (privateKey != null) {
jsch.addIdentity(privateKey);// 设置私钥
}
session = jsch.getSession(username, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60 * 1000 * 10);
System.out.println("session:"+session);
Channel channel = session.openChannel("sftp");
channel.connect(60 * 1000 * 10);
sftp = (ChannelSftp) channel;
System.out.println("login-sftp:" + sftp);
} catch (JSchException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 关闭连接 server
*/
public void logout() {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
/**
* 将输入流的数据上传到sftp作为文件文件完整路径=basePath+directory
*
* @param directory 上传到该目录
* @param sftpFileName sftp端文件名
*/
public boolean upload(String directory, String sftpFileName, InputStream input) throws SftpException {
System.out.println("upload-sftp:" + sftp);
try {
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
sftp.put(input, sftpFileName); //上传文件
return true;
} catch (SftpException e) {
return false;
}
}
public void cd(String directory) throws SftpException {
if (directory != null && !"".equals(directory) && !"/".equals(directory)) {
sftp.cd(directory);
}
}
/**
* 下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
*/
public void download(String directory, String downloadFile, String saveFile) {
System.out.println("download:" + directory + " downloadFile:" + downloadFile + " saveFile:" + saveFile);
File file = null;
try {
if (directory != null && !"".equals(directory)) {
System.out.println(sftp);
sftp.cd(directory);
}
file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (SftpException | FileNotFoundException e) {
e.printStackTrace();
if (file != null) {
file.delete();
}
}
}
/**
* 删除文件
*
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
*/
public void delete(String directory, String deleteFile) throws SftpException {
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
sftp.rm(deleteFile);
}
/**
* 列出目录下的文件
*
* @param directory 要列出的目录
*/
public List<String> listFiles(String directory) throws SftpException {
Vector<?> objects = sftp.ls(directory);
List<String> list = new ArrayList<>();
for (Object s : objects){
String x = s.toString();
String[] s1 = x.split(" ");
System.out.println(s1[s1.length -1]);
}
return list;
}
public boolean isExistsFile(String directory, String fileName) {
List<String> findFileList = new ArrayList<>();
ChannelSftp.LsEntrySelector selector = lsEntry -> {
if (lsEntry.getFilename().equals(fileName)) {
findFileList.add(fileName);
}
return 0;
};
try {
sftp.ls(directory, selector);
} catch (SftpException e) {
e.printStackTrace();
}
return findFileList.size() > 0;
}
}

View File

@ -0,0 +1,56 @@
package org.jeecg.baseAPI.sftpAPI.impl;
import org.jeecg.baseAPI.sftpAPI.IBizSftpAPIService;
import org.jeecg.baseAPI.sftpAPI.SFTPUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* @Description: 通用API
* @Author: jeecg-boot
* @Date: 2023-09-20
* @Version: V1.0
*/
@Service
public class BizSftpAPIServiceImpl implements IBizSftpAPIService {
@Value("${spring.Linux.ip}")
private String ip;
@Value("${spring.Linux.username}")
private String username;
@Value("${spring.Linux.password}")
private String password;
@Value("${spring.Linux.port}")
private Integer port;
@Override
public void sftpUpload(String file, String cshFilePath, String fileName) {
try{
SFTPUtil sftpUtil = new SFTPUtil();
sftpUtil.login(username, password,ip,port);
InputStream inputStream = new FileInputStream(file);
sftpUtil.upload(cshFilePath,fileName,inputStream);
sftpUtil.logout();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void download(String directory, String downloadFile, String saveFile) {
try{
SFTPUtil sftpUtil = new SFTPUtil();
sftpUtil.login(username, password,ip,port);
sftpUtil.download(directory,downloadFile,saveFile);
sftpUtil.logout();
}catch (Exception e){
e.printStackTrace();
}
}
}