测试sftp

main
youHong.ai 2022-11-22 17:54:34 +08:00
parent 09cdc32bf8
commit 25db7d1f55
2 changed files with 18 additions and 12 deletions

View File

@ -2,12 +2,11 @@ package weaver.youhong.ai.pcn.hrorganization.sftp;
import aiyh.utils.excention.CustomerException; import aiyh.utils.excention.CustomerException;
import aiyh.utils.fileUtil.sftp.SftpConnectUtil; import aiyh.utils.fileUtil.sftp.SftpConnectUtil;
import cn.hutool.core.io.IoUtil;
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpException;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Vector; import java.util.Vector;
/** /**
@ -36,20 +35,25 @@ public class FetchDataUtil {
public void downloadFile(SftpConnectUtil sftpConnectUtil,String fileName, public void downloadFile(SftpConnectUtil sftpConnectUtil,String fileName,
String targetFile){ String targetFile){
try { try {
sftpConnectUtil.cd("/"); sftpConnectUtil.lcd(".");
Vector<ChannelSftp.LsEntry> ls = sftpConnectUtil.ls("/"); // Vector ls = sftpConnectUtil.ls(".");
for (ChannelSftp.LsEntry l : ls) { // for (Object l : ls) {
System.out.println(l.getFilename()); // System.out.println(l);
} // }
//
File file = new File(targetFile); File file = new File(targetFile);
if (!file.exists()) { if (!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
sftpConnectUtil.get(fileName + "/", new FileOutputStream(file)); sftpConnectUtil.get(fileName,new FileOutputStream(targetFile));
} catch (SftpException | FileNotFoundException e) {
} catch (SftpException e) {
sftpConnectUtil.close();
e.printStackTrace(); e.printStackTrace();
// throw new CustomerException("下载文件出错,down file error!"); // throw new CustomerException("下载文件出错,down file error!");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} }
} }

View File

@ -2,6 +2,7 @@ package youhong.ai.pcn;
import aiyh.utils.fileUtil.sftp.SftpConnectUtil; import aiyh.utils.fileUtil.sftp.SftpConnectUtil;
import baseTest.BaseTest; import baseTest.BaseTest;
import com.api.doc.detail.util.SFTPUtils;
import org.junit.Test; import org.junit.Test;
import weaver.general.GCONST; import weaver.general.GCONST;
import weaver.youhong.ai.pcn.hrorganization.sftp.FetchDataUtil; import weaver.youhong.ai.pcn.hrorganization.sftp.FetchDataUtil;
@ -22,8 +23,9 @@ public class TestOrganization extends BaseTest {
SftpConnectUtil sftpConnectUtil = new SftpConnectUtil( SftpConnectUtil sftpConnectUtil = new SftpConnectUtil(
"HR Digital_PROD", "HR Digital_PROD",
"/Users/aoey.oct.22/company/Fan_wei/ssl/pcn/HR_Digital_PROD.pem",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 null,1000 * 100
); );
SFTPUtils sftpUtils = new SFTPUtils();
FetchDataUtil fetchDataUtil = new FetchDataUtil(); FetchDataUtil fetchDataUtil = new FetchDataUtil();
fetchDataUtil.downloadFile(sftpConnectUtil, fetchDataUtil.downloadFile(sftpConnectUtil,
"HRIS_DepartmentExport20200503", GCONST.getSysFilePath() + "HRIS_DepartmentExport20200503.csv"); "HRIS_DepartmentExport20200503", GCONST.getSysFilePath() + "HRIS_DepartmentExport20200503.csv");