测试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.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<ChannelSftp.LsEntry> 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);
}
}

View File

@ -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");