diff --git a/.gitignore b/.gitignore index 5edce1b..cc07bbd 100644 --- a/.gitignore +++ b/.gitignore @@ -24,10 +24,11 @@ log /target/ /log/ -/test/java/baseTest/BaseTest.java +/src/test/java/baseTest/BaseTest.java .DS_Store /src/main/resources/WEB-INF/prop/weaver.properties /lib/classbean +/file/ diff --git a/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java b/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java index 5456dd0..61c5d3b 100644 --- a/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java +++ b/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java @@ -94,7 +94,7 @@ public class SftpConnectUtil extends ChannelSftp { } else { throw new RuntimeException("从sftp下载需要的ppk文件未找到"); } - if (port > 0) { + if (port != null && port > 0) { this.session = jSch.getSession(userName, sftpIp, port); } else { this.session = jSch.getSession(userName, sftpIp); 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 234418f..0a99315 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,8 +2,14 @@ package weaver.youhong.ai.pcn.hrorganization.sftp; import aiyh.utils.excention.CustomerException; import aiyh.utils.fileUtil.sftp.SftpConnectUtil; +import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.SftpException; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Vector; + /** *

获取数据工具

* @@ -30,9 +36,20 @@ public class FetchDataUtil { public void downloadFile(SftpConnectUtil sftpConnectUtil,String fileName, String targetFile){ try { - sftpConnectUtil.get(fileName, targetFile); - } catch (SftpException e) { - throw new CustomerException("下载文件出错,down file error!"); + sftpConnectUtil.cd("/"); + Vector ls = sftpConnectUtil.ls("/"); + for (ChannelSftp.LsEntry l : ls) { + System.out.println(l.getFilename()); + } + + File file = new File(targetFile); + if (!file.exists()) { + file.getParentFile().mkdirs(); + } + sftpConnectUtil.get(fileName + "/", new FileOutputStream(file)); + } catch (SftpException | FileNotFoundException e) { + e.printStackTrace(); + // throw new CustomerException("下载文件出错,down file error!"); } } diff --git a/src/test/java/youhong/ai/pcn/TestOrganization.java b/src/test/java/youhong/ai/pcn/TestOrganization.java index 53245e0..6217afa 100644 --- a/src/test/java/youhong/ai/pcn/TestOrganization.java +++ b/src/test/java/youhong/ai/pcn/TestOrganization.java @@ -21,7 +21,7 @@ public class TestOrganization extends BaseTest { public void testSftp(){ SftpConnectUtil sftpConnectUtil = new SftpConnectUtil( "HR Digital_PROD", - "/Users/aoey.oct.22/company/Fan_wei/ssl/pcn/HR_Digital_PROD.ppk",null,"222.73.197.242", + "/Users/aoey.oct.22/company/Fan_wei/ssl/pcn/HR_Digital_PROD.pem",null,"222.73.197.242", null,1000 * 10 ); FetchDataUtil fetchDataUtil = new FetchDataUtil();