diff --git a/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java b/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java deleted file mode 100644 index 7af9199..0000000 --- a/src/main/java/aiyh/utils/fileUtil/sftp/SftpConnectUtil.java +++ /dev/null @@ -1,141 +0,0 @@ -package aiyh.utils.fileUtil.sftp; - -import aiyh.utils.excention.CustomerException; -import com.jcraft.jsch.*; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import java.util.Properties; - -/** - *

sftp连接工具类

- * - *

create: 2022-11-22 11:00

- * - * @author youHong.ai - */ - -@Getter -@NoArgsConstructor -public class SftpConnectUtil extends ChannelSftp { - - /** - * sftp对象 - */ - private ChannelSftp sftp; - /** - * 会话对象 - */ - private Session session; - /** - * 用户名 - */ - private String userName; - - /** - * 密码 - */ - private String password; - - /** - * 密钥地址 - */ - private String prvKeyFilePath; - - /** - * 是否成功 - */ - private boolean success; - - /** - * sftp地址 - */ - private String sftpIp; - - /** - * 端口 - */ - private Integer port; - - /** - * 超时事件 - */ - private Integer sftpTimeOut; - - - public SftpConnectUtil(String userName, String prvKeyFilePath,String password, String sftpIp, - Integer port, Integer sftpTimeOut) { - this.userName = userName; - this.prvKeyFilePath = prvKeyFilePath; - this.sftpIp = sftpIp; - this.port = port; - this.password = password; - this.sftpTimeOut = sftpTimeOut; - // this.login(); - } - - - /** - *

登陆sftp

- * - * @author youHong.ai - */ - - public ChannelSftp login() { - - JSch jSch = new JSch(); - try { - if (prvKeyFilePath != null && !"".equals(prvKeyFilePath)) { - if (password != null && !"".equals(password)) { - jSch.addIdentity(prvKeyFilePath, password); - } else { - jSch.addIdentity(prvKeyFilePath); - } - } else { - throw new RuntimeException("从sftp下载需要的ppk文件未找到"); - } - if (port != null && port > 0) { - this.session = jSch.getSession(userName, sftpIp, port); - } else { - this.session = jSch.getSession(userName, sftpIp); - } - Properties config = new Properties(); - config.put("StrictHostKeyChecking", "no"); - this.session.setConfig(config); - this.session.setTimeout(sftpTimeOut); - this.session.connect(); - Channel channel = this.session.openChannel("sftp"); - channel.connect(); - this.sftp = (ChannelSftp) channel; - this.success = true; - return sftp; - } catch (JSchException e) { - throw new CustomerException("SFTP链接失败!", e); - } - } - - /** - * 关闭连接 server - * - * @author youHong.ai - */ - private void logout() { - if (sftp != null) { - if (sftp.isConnected()) { - sftp.disconnect(); - } - } - if (session != null) { - if (session.isConnected()) { - session.disconnect(); - } - } - } - - /** - *

关闭链接释放资源

- */ - public void close(){ - this.login(); - } -} diff --git a/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java b/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java new file mode 100644 index 0000000..3395123 --- /dev/null +++ b/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java @@ -0,0 +1,571 @@ +package ebu7common.youhong.ai.sftp; + +import aiyh.utils.excention.CustomerException; +import com.jcraft.jsch.*; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Hashtable; +import java.util.Properties; +import java.util.Vector; + +/** + *

sftp连接工具类

+ * + *

create: 2022-11-22 11:00

+ * + * @author youHong.ai + */ + +@Getter +@NoArgsConstructor +public class SftpConnectUtil extends ChannelSftp { + + /** + * sftp对象 + */ + private ChannelSftp sftp; + /** + * 会话对象 + */ + private Session session; + /** + * 用户名 + */ + private String userName; + + /** + * 密码 + */ + private String password; + + /** + * 密钥地址 + */ + private String prvKeyFilePath; + + /** + * 是否成功 + */ + private boolean success; + + /** + * sftp地址 + */ + private String sftpIp; + + /** + * 端口 + */ + private Integer port; + + /** + * 超时事件 + */ + private Integer sftpTimeOut; + + + public SftpConnectUtil(String userName, String prvKeyFilePath,String password, String sftpIp, + Integer port, Integer sftpTimeOut) { + this.userName = userName; + this.prvKeyFilePath = prvKeyFilePath; + this.sftpIp = sftpIp; + this.port = port; + this.password = password; + this.sftpTimeOut = sftpTimeOut; + this.login(); + } + + + /** + *

登陆sftp

+ * + * @author youHong.ai + */ + + private void login() { + + JSch jSch = new JSch(); + try { + if (prvKeyFilePath != null && !"".equals(prvKeyFilePath)) { + if (password != null && !"".equals(password)) { + jSch.addIdentity(prvKeyFilePath, password); + } else { + jSch.addIdentity(prvKeyFilePath); + } + } else { + throw new RuntimeException("从sftp下载需要的ppk文件未找到"); + } + if (port != null && port > 0) { + this.session = jSch.getSession(userName, sftpIp, port); + } else { + this.session = jSch.getSession(userName, sftpIp); + } + Properties config = new Properties(); + config.put("StrictHostKeyChecking", "no"); + this.session.setConfig(config); + this.session.setTimeout(sftpTimeOut); + this.session.connect(); + Channel channel = this.session.openChannel("sftp"); + channel.connect(); + this.sftp = (ChannelSftp) channel; + this.success = true; + } catch (JSchException e) { + throw new CustomerException("SFTP链接失败!", e); + } + } + + /** + * 关闭连接 server + * + * @author youHong.ai + */ + private void logout() { + if (sftp != null) { + if (sftp.isConnected()) { + sftp.disconnect(); + } + } + if (session != null) { + if (session.isConnected()) { + session.disconnect(); + } + } + } + + /** + *

关闭链接释放资源

+ */ + public void close(){ + this.login(); + } + + @Override + public boolean isClosed() { + return this.sftp.isClosed(); + } + + @Override + public void setBulkRequests(int bulk_requests) throws JSchException { + this.sftp.setBulkRequests(bulk_requests); + } + + @Override + public int getBulkRequests() { + return this.sftp.getBulkRequests(); + } + + @Override + public void start() throws JSchException { + this.sftp.start(); + } + + @Override + public boolean isEOF() { + return this.sftp.isEOF(); + } + + @Override + public void setInputStream(InputStream in) { + this.sftp.setInputStream(in); + } + + @Override + public void setInputStream(InputStream in, boolean dontclose) { + this.sftp.setInputStream(in, dontclose); + } + + @Override + public void setOutputStream(OutputStream out) { + this.sftp.setOutputStream(out); + } + + @Override + public void setOutputStream(OutputStream out, boolean dontclose) { + this.sftp.setOutputStream(out, dontclose); + } + + @Override + public void setExtOutputStream(OutputStream out) { + this.sftp.setExtOutputStream(out); + } + + @Override + public void setExtOutputStream(OutputStream out, boolean dontclose) { + this.sftp.setExtOutputStream(out, dontclose); + } + + @Override + public InputStream getInputStream() throws IOException { + return this.sftp.getInputStream(); + } + + @Override + public InputStream getExtInputStream() throws IOException { + return this.sftp.getExtInputStream(); + } + + @Override + public OutputStream getOutputStream() throws IOException { + return this.sftp.getOutputStream(); + } + + @Override + public void quit() { + this.sftp.quit(); + } + + @Override + public void exit() { + this.sftp.exit(); + } + + @Override + public void lcd(String path) throws SftpException { + this.sftp.lcd(path); + } + + @Override + public void cd(String path) throws SftpException { + this.sftp.cd(path); + } + + @Override + public void put(String src, String dst) throws SftpException { + this.sftp.put(src, dst); + } + + @Override + public void put(String src, String dst, int mode) throws SftpException { + this.sftp.put(src, dst, mode); + } + + @Override + public void put(String src, String dst, SftpProgressMonitor monitor) throws SftpException { + this.sftp.put(src, dst, monitor); + } + + @Override + public void put(String src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException { + this.sftp.put(src, dst, monitor, mode); + } + + @Override + public void put(InputStream src, String dst) throws SftpException { + this.sftp.put(src, dst); + } + + @Override + public void put(InputStream src, String dst, int mode) throws SftpException { + this.sftp.put(src, dst, mode); + } + + @Override + public void put(InputStream src, String dst, SftpProgressMonitor monitor) throws SftpException { + this.sftp.put(src, dst, monitor); + } + + @Override + public void put(InputStream src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException { + this.sftp.put(src, dst, monitor, mode); + } + + @Override + public void _put(InputStream src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException { + this.sftp._put(src, dst, monitor, mode); + } + + @Override + public OutputStream put(String dst) throws SftpException { + return this.sftp.put(dst); + } + + @Override + public OutputStream put(String dst, int mode) throws SftpException { + return this.sftp.put(dst, mode); + } + + @Override + public OutputStream put(String dst, SftpProgressMonitor monitor, int mode) throws SftpException { + return this.sftp.put(dst, monitor, mode); + } + + @Override + public OutputStream put(String dst, SftpProgressMonitor monitor, int mode, long offset) throws SftpException { + return this.sftp.put(dst, monitor, mode, offset); + } + + @Override + public void get(String src, String dst) throws SftpException { + this.sftp.get(src, dst); + } + + @Override + public void get(String src, String dst, SftpProgressMonitor monitor) throws SftpException { + this.sftp.get(src, dst, monitor); + } + + @Override + public void get(String src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException { + this.sftp.get(src, dst, monitor, mode); + } + + @Override + public void get(String src, OutputStream dst) throws SftpException { + this.sftp.get(src, dst); + } + + @Override + public void get(String src, OutputStream dst, SftpProgressMonitor monitor) throws SftpException { + this.sftp.get(src, dst, monitor); + } + + @Override + public void get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) throws SftpException { + this.sftp.get(src, dst, monitor, mode, skip); + } + + @Override + public InputStream get(String src) throws SftpException { + return this.sftp.get(src); + } + + @Override + public InputStream get(String src, SftpProgressMonitor monitor) throws SftpException { + return this.sftp.get(src, monitor); + } + + @Override + public InputStream get(String src, int mode) throws SftpException { + return this.sftp.get(src, mode); + } + + @Override + public InputStream get(String src, SftpProgressMonitor monitor, int mode) throws SftpException { + return this.sftp.get(src, monitor, mode); + } + + @Override + public InputStream get(String src, SftpProgressMonitor monitor, long skip) throws SftpException { + return this.sftp.get(src, monitor, skip); + } + + @Override + public Vector ls(String path) throws SftpException { + return this.sftp.ls(path); + } + + @Override + public void ls(String path, LsEntrySelector selector) throws SftpException { + this.sftp.ls(path, selector); + } + + @Override + public String readlink(String path) throws SftpException { + return this.sftp.readlink(path); + } + + @Override + public void symlink(String oldpath, String newpath) throws SftpException { + this.sftp.symlink(oldpath, newpath); + } + + @Override + public void hardlink(String oldpath, String newpath) throws SftpException { + this.sftp.hardlink(oldpath, newpath); + } + + @Override + public void rename(String oldpath, String newpath) throws SftpException { + this.sftp.rename(oldpath, newpath); + } + + @Override + public void rm(String path) throws SftpException { + this.sftp.rm(path); + } + + @Override + public void chgrp(int gid, String path) throws SftpException { + this.sftp.chgrp(gid, path); + } + + @Override + public void chown(int uid, String path) throws SftpException { + this.sftp.chown(uid, path); + } + + @Override + public void chmod(int permissions, String path) throws SftpException { + this.sftp.chmod(permissions, path); + } + + @Override + public void setMtime(String path, int mtime) throws SftpException { + this.sftp.setMtime(path, mtime); + } + + @Override + public void rmdir(String path) throws SftpException { + this.sftp.rmdir(path); + } + + @Override + public void mkdir(String path) throws SftpException { + this.sftp.mkdir(path); + } + + @Override + public SftpATTRS stat(String path) throws SftpException { + return this.sftp.stat(path); + } + + @Override + public SftpStatVFS statVFS(String path) throws SftpException { + return this.sftp.statVFS(path); + } + + @Override + public SftpATTRS lstat(String path) throws SftpException { + return this.sftp.lstat(path); + } + + @Override + public void setStat(String path, SftpATTRS attr) throws SftpException { + this.sftp.setStat(path, attr); + } + + @Override + public String pwd() throws SftpException { + return this.sftp.pwd(); + } + + @Override + public String lpwd() { + return this.sftp.lpwd(); + } + + @Override + public String version() { + return this.sftp.version(); + } + + @Override + public String getHome() throws SftpException { + return this.sftp.getHome(); + } + + @Override + public void disconnect() { + this.sftp.disconnect(); + } + + @Override + public boolean isConnected() { + return this.sftp.isConnected(); + } + + @Override + public void sendSignal(String signal) throws Exception { + this.sftp.sendSignal(signal); + } + + @Override + public int getExitStatus() { + return this.sftp.getExitStatus(); + } + + @Override + public int getId() { + return this.sftp.getId(); + } + + @Override + public int getServerVersion() throws SftpException { + return this.sftp.getServerVersion(); + } + + @Override + public void setFilenameEncoding(String encoding) throws SftpException { + this.sftp.setFilenameEncoding(encoding); + } + + @Override + public String getExtension(String key) { + return this.sftp.getExtension(key); + } + + @Override + public String realpath(String path) throws SftpException { + return this.sftp.realpath(path); + } + + @Override + public void setAgentForwarding(boolean enable) { + this.sftp.setAgentForwarding(enable); + } + + @Override + public void connect() throws JSchException { + this.sftp.connect(); + } + + @Override + public void connect(int connectTimeout) throws JSchException { + this.sftp.connect(connectTimeout); + } + + @Override + public void setXForwarding(boolean enable) { + this.sftp.setXForwarding(enable); + } + + @Override + public void setEnv(Hashtable env) { + this.sftp.setEnv(env); + } + + @Override + public void setEnv(String name, String value) { + this.sftp.setEnv(name, value); + } + + @Override + public void setEnv(byte[] name, byte[] value) { + this.sftp.setEnv(name, value); + } + + @Override + public void setPty(boolean enable) { + this.sftp.setPty(enable); + } + + @Override + public void setTerminalMode(byte[] terminal_mode) { + this.sftp.setTerminalMode(terminal_mode); + } + + @Override + public void setPtySize(int col, int row, int wp, int hp) { + this.sftp.setPtySize(col, row, wp, hp); + } + + @Override + public void setPtyType(String ttype) { + this.sftp.setPtyType(ttype); + } + + @Override + public void setPtyType(String ttype, int col, int row, int wp, int hp) { + this.sftp.setPtyType(ttype, col, row, wp, hp); + } + + + @Override + public void run() { + this.sftp.run(); + } +} 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 b7a9e48..713f78e 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 @@ -1,13 +1,10 @@ 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 ebu7common.youhong.ai.sftp.SftpConnectUtil; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.SftpException; import java.io.*; -import java.util.Vector; /** *

获取数据工具

@@ -32,7 +29,7 @@ public class FetchDataUtil { - public void downloadFile(ChannelSftp sftp,SftpConnectUtil sftpConnectUtil, + public void downloadFile(SftpConnectUtil sftpConnectUtil, String fileName, String targetFile){ try { // sftpConnectUtil.lcd("."); @@ -45,7 +42,7 @@ public class FetchDataUtil { if (!file.exists()) { file.getParentFile().mkdirs(); } - sftp.get(fileName,new FileOutputStream(targetFile)); + sftpConnectUtil.get(fileName,new FileOutputStream(targetFile)); } catch (SftpException e) { diff --git a/src/test/java/youhong/ai/pcn/TestOrganization.java b/src/test/java/youhong/ai/pcn/TestOrganization.java index 48ff058..21963cd 100644 --- a/src/test/java/youhong/ai/pcn/TestOrganization.java +++ b/src/test/java/youhong/ai/pcn/TestOrganization.java @@ -1,8 +1,7 @@ package youhong.ai.pcn; -import aiyh.utils.fileUtil.sftp.SftpConnectUtil; +import ebu7common.youhong.ai.sftp.SftpConnectUtil; import baseTest.BaseTest; -import com.api.doc.detail.util.SFTPUtils; import com.jcraft.jsch.ChannelSftp; import org.junit.Test; import weaver.general.GCONST; @@ -26,9 +25,8 @@ public class TestOrganization extends BaseTest { "/Users/aoey.oct.22/company/Fan_wei/ssl/pcn/HR_Digital_PROD.pem",null,"222.73.197.242", null,1000 * 100 ); - ChannelSftp login = sftpConnectUtil.login(); FetchDataUtil fetchDataUtil = new FetchDataUtil(); - fetchDataUtil.downloadFile(login, sftpConnectUtil, + fetchDataUtil.downloadFile(sftpConnectUtil, "HRIS_PositionExport20221120", GCONST.getSysFilePath() + "HRIS_PositionExport20221120.csv"); } }