|
@@ -4,10 +4,12 @@ import com.jcraft.jsch.ChannelSftp;
|
|
|
import com.jcraft.jsch.JSch;
|
|
|
import com.jcraft.jsch.Session;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.Properties;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @description:
|
|
@@ -20,7 +22,6 @@ public class SFTPUtil {
|
|
|
static JSch jsch = null;
|
|
|
static Session session = null;
|
|
|
static ChannelSftp channel = null;
|
|
|
-
|
|
|
/**
|
|
|
* 密钥连接到指定的IP
|
|
|
*
|
|
@@ -58,7 +59,7 @@ public class SFTPUtil {
|
|
|
/**
|
|
|
* 关闭连接
|
|
|
*/
|
|
|
- private static void close() {
|
|
|
+ public static void close() {
|
|
|
if (channel != null) {
|
|
|
try {
|
|
|
channel.disconnect();
|
|
@@ -98,5 +99,83 @@ public class SFTPUtil {
|
|
|
// todo 秘钥文件路径有问题 需要测试上传 下载需要在util中写个方法,替换ftp文件夹下的route
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ * 备份本地文件,再下载服务器端最新文件
|
|
|
+ * @param localPath 本地存放路径
|
|
|
+ * @param remotePath 服务器端存放路径
|
|
|
+ */
|
|
|
+ public static void download(String username, int port, String host, String passphrase, String keyFilePath,String localPath, String remotePath) throws Exception {
|
|
|
+ // src linux服务器文件地址,dst 本地存放地址
|
|
|
+ connect(username, host, port, passphrase, keyFilePath);
|
|
|
+ if (channel == null) {
|
|
|
+ log.info("SFTP服务器未连接");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ FileOutputStream output;
|
|
|
+
|
|
|
+ // 获取未读取文件
|
|
|
+ try {
|
|
|
+ List<String> files = listFiles(remotePath);
|
|
|
+ for (String fileName : files) {
|
|
|
+ output = new FileOutputStream(localPath + "/" + fileName);
|
|
|
+ log.info("下载文件:" + fileName + "开始");
|
|
|
+ channel.get(remotePath + "/" +fileName, output);
|
|
|
+ log.info("下载文件:" + fileName + "成功");
|
|
|
+ delete(remotePath, fileName);
|
|
|
+ output.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("下载文件失败,失败原因:{}", e.getMessage());
|
|
|
+ } finally {
|
|
|
+ close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ *
|
|
|
+ * @param directory
|
|
|
+ * 要删除文件所在目录
|
|
|
+ * @param deleteFile
|
|
|
+ * 要删除的文件
|
|
|
+ * @throws JSchException
|
|
|
+ */
|
|
|
+ private static void delete(String directory, String deleteFile) throws Exception {
|
|
|
+ if (channel == null) {
|
|
|
+ log.info("SFTP服务器未连接");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ channel.cd(directory);
|
|
|
+ channel.rm(deleteFile);
|
|
|
+ log.info("删除成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列出目录下的文件
|
|
|
+ *
|
|
|
+ * @param directory
|
|
|
+ * 要列出的目录
|
|
|
+ * @param sftp
|
|
|
+ * @return
|
|
|
+ * @throws JSchException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ private static List<String> listFiles(String directory) throws Exception {
|
|
|
+ if (channel == null) {
|
|
|
+ log.info("SFTP服务器未连接");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Vector vector = channel.ls(directory);
|
|
|
|
|
|
+ List<String> fileNameList = new ArrayList<>();
|
|
|
+ for (Object object : vector) {
|
|
|
+ String fileName = ((ChannelSftp.LsEntry) object).getFilename();
|
|
|
+ String[] array = StringUtils.split(fileName,".");
|
|
|
+ if (array.length == 2 && (array[1].equals("xlsx") || array[1].equals("xls"))) {
|
|
|
+ fileNameList.add(fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fileNameList;
|
|
|
+ }
|
|
|
}
|