多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 组件源码 ``` /** * <b>方法描述:</b> SFTP文件下载 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2018-05-08 18:25:18 <br/> * * @param host * 入参|服务器地址|{@link java.lang.String} * @param port * 入参|服务器端口|{@link java.lang.Integer} * @param username * 入参|用户|{@link java.lang.String} * @param password * 入参|密码|{@link java.lang.String} * @param remoteFile * 入参|服务器文件|{@link java.lang.String} * @param localFile * 入参|本地目录|{@link java.lang.String} * @return -1 异常<br/> * 1 成功<br/> */ @Component(label = "SFTP文件下载", style = "判断型", type = "同步组件", comment = "SFTP文件下载", version = "1.0.0", deprecated = false, author = "admin", date = "2018-05-08 06:25:18") @InParams(param = { @Param(name = "host", comment = "服务器地址", type = java.lang.String.class), @Param(name = "port", comment = "服务器端口", type = java.lang.Integer.class), @Param(name = "username", comment = "用户", type = java.lang.String.class), @Param(name = "password", comment = "密码", type = java.lang.String.class), @Param(name = "remoteFile", comment = "服务器文件", type = java.lang.String.class), @Param(name = "localFile", comment = "本地目录", type = java.lang.String.class) }) @Returns(returns = { @Return(id = "-1", desp = "异常"), @Return(id = "1", desp = "成功") }) public static ResultBase P_sftpDownload(String host, Integer port, String username, String password, String remoteFile, String localFile) { ChannelSftp sftp = null; Session session = null; String[] strATmp = remoteFile.replaceAll("\\\\", "/").split("/"); String svrFile = strATmp[strATmp.length - 1]; String svrPath = remoteFile.substring(0, remoteFile.length() - svrFile.length()); if (StringUtils.isEmpty(localFile)) { localFile = svrFile; } else { localFile = localFile.replaceAll("\\\\", "/"); if (localFile.endsWith("/")) { localFile += svrFile; } File ff = new File(new File(localFile).getParent()); ff.mkdirs(); } try { try { JSch jsch = new JSch(); session = jsch.getSession(username, host, port); if (password != null) { session.setPassword(password); } Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (Exception e) { AppLog.error(e); throw new Exception("登录SFTP服务器失败"); } if (svrPath != null && !"".equals(svrPath)) { sftp.cd(svrPath); } File file = new File(localFile); sftp.get(svrFile, new FileOutputStream(file)); return ResultBase.newSuccessResult(); } catch (Exception ex) { AppLog.error(ex); return ResultBase.newExceptionResult("TPTX0017", "SFTP文件下载异常:"+AppLog.errorMsg(ex)); } finally { if (sftp != null && sftp.isConnected()) { sftp.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/26/28/2628f57278ada8a6575713a71c3c03c9_1861x887.png) # 参数说明及示例 ## 入口参数 服务器地址:SFTP 所在服务地址,示例: `__REQ__["serverName"]` 服务器端口:SFTP 服务端口,示例: `__REQ__["port"]` 用户:SFTP 用户名,示例: `__REQ__["user"]` 密码:SFTP 密码,示例: `__REQ__["password"]` 服务器文件:SFTP 服务器目录中的文件,示例: `"/opt/abc.txt"` 本地目录:下载到本地哪个目录,示例: `"D:\\work\\CQYY\\workspace"`