From 25db7d1f557fcabd5d926becf3240a800816e7a5 Mon Sep 17 00:00:00 2001 From: "youHong.ai" <774495953@qq.com> Date: Tue, 22 Nov 2022 17:54:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95sftp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hrorganization/sftp/FetchDataUtil.java | 26 +++++++++++-------- .../java/youhong/ai/pcn/TestOrganization.java | 4 ++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java b/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java index 0a99315..5b4ee07 100644 --- a/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java +++ b/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java @@ -2,12 +2,11 @@ package weaver.youhong.ai.pcn.hrorganization.sftp; import aiyh.utils.excention.CustomerException; import aiyh.utils.fileUtil.sftp.SftpConnectUtil; +import cn.hutool.core.io.IoUtil; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.SftpException; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; +import java.io.*; import java.util.Vector; /** @@ -36,20 +35,25 @@ public class FetchDataUtil { public void downloadFile(SftpConnectUtil sftpConnectUtil,String fileName, String targetFile){ try { - sftpConnectUtil.cd("/"); - Vector ls = sftpConnectUtil.ls("/"); - for (ChannelSftp.LsEntry l : ls) { - System.out.println(l.getFilename()); - } - + sftpConnectUtil.lcd("."); + // Vector ls = sftpConnectUtil.ls("."); + // for (Object l : ls) { + // System.out.println(l); + // } + // File file = new File(targetFile); if (!file.exists()) { file.getParentFile().mkdirs(); } - sftpConnectUtil.get(fileName + "/", new FileOutputStream(file)); - } catch (SftpException | FileNotFoundException e) { + sftpConnectUtil.get(fileName,new FileOutputStream(targetFile)); + + + } catch (SftpException e) { + sftpConnectUtil.close(); e.printStackTrace(); // throw new CustomerException("下载文件出错,down file error!"); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); } } diff --git a/src/test/java/youhong/ai/pcn/TestOrganization.java b/src/test/java/youhong/ai/pcn/TestOrganization.java index 6217afa..68246c2 100644 --- a/src/test/java/youhong/ai/pcn/TestOrganization.java +++ b/src/test/java/youhong/ai/pcn/TestOrganization.java @@ -2,6 +2,7 @@ package youhong.ai.pcn; import aiyh.utils.fileUtil.sftp.SftpConnectUtil; import baseTest.BaseTest; +import com.api.doc.detail.util.SFTPUtils; import org.junit.Test; import weaver.general.GCONST; import weaver.youhong.ai.pcn.hrorganization.sftp.FetchDataUtil; @@ -22,8 +23,9 @@ public class TestOrganization extends BaseTest { SftpConnectUtil sftpConnectUtil = new SftpConnectUtil( "HR Digital_PROD", "/Users/aoey.oct.22/company/Fan_wei/ssl/pcn/HR_Digital_PROD.pem",null,"222.73.197.242", - null,1000 * 10 + null,1000 * 100 ); + SFTPUtils sftpUtils = new SFTPUtils(); FetchDataUtil fetchDataUtil = new FetchDataUtil(); fetchDataUtil.downloadFile(sftpConnectUtil, "HRIS_DepartmentExport20200503", GCONST.getSysFilePath() + "HRIS_DepartmentExport20200503.csv");