ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 组件源码 ``` /** * <b>方法描述:</b> FTP文件下载 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2018-05-08 18:22:33 <br/> * * @param ftpHost * 入参|FTP服务器地址|{@link java.lang.String} * @param ftpPort * 入参|FTP服务器端口|{@link java.lang.Integer} * @param ftpUser * 入参|FTP用户|{@link java.lang.String} * @param ftpPswd * 入参|FTP密码|{@link java.lang.String} * @param remoteFile * 入参|服务器文件|{@link java.lang.String} * @param localFile * 入参|本地目录|{@link java.lang.String} * @return -1 异常<br/> * 0 失败<br/> * 1 成功<br/> */ @Component(label = "FTP文件下载", style = "判断型", type = "同步组件", comment = "FTP文件下载", version = "1.0.0", deprecated = false, author = "admin", date = "2018-05-08 06:22:33") @InParams(param = { @Param(name = "ftpHost", comment = "FTP服务器地址", type = java.lang.String.class), @Param(name = "ftpPort", comment = "FTP服务器端口", type = java.lang.Integer.class), @Param(name = "ftpUser", comment = "FTP用户", type = java.lang.String.class), @Param(name = "ftpPswd", comment = "FTP密码", 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 = "0", desp = "失败"), @Return(id = "1", desp = "成功") }) public static ResultBase P_ftpDownload(String ftpHost, Integer ftpPort, String ftpUser, String ftpPswd, String remoteFile, String localFile) { FTPClient ftp = new FTPClient(); ftp.setControlEncoding("GBK"); FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); conf.setServerLanguageCode("zh"); ftp.configure(conf); 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 { int reply; ftp.connect(ftpHost, ftpPort); ftp.login(ftpUser, ftpPswd); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return ResultBase.newFailureResult("TPTX0006", "登录FTP服务失败"); } if (StringUtils.isNotEmpty(svrPath) && !ftp.changeWorkingDirectory(svrPath)) { ftp.disconnect(); return ResultBase.newFailureResult("TPTX0007", "远程目录" + svrPath + "不存在"); } ftp.setFileType(FTP.BINARY_FILE_TYPE); FTPFile[] fs = ftp.listFiles(svrFile); if (fs == null || fs.length < 1) { ftp.disconnect(); return ResultBase.newFailureResult("TPTX0008", "远程文件" + svrFile + "不存在"); } for (FTPFile ff : fs) { File fileObj = new File(localFile); OutputStream is = new FileOutputStream(fileObj); ftp.retrieveFile(ff.getName(), is); is.close(); } ftp.logout(); return ResultBase.newSuccessResult(); } catch (Exception e) { AppLog.error(e); return ResultBase.newExceptionResult("TPTX0009", "FTP下载文件失败," + AppLog.errorMsg(e)); } finally { try { ftp.disconnect(); } catch (Exception ioe) { AppLog.error(ioe); } } } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/fa/35/fa358fa51dd626332b36793837d895e1_1868x891.png) # 参数说明及示例 ## 入口参数 FTP服务器地址:FTP 所在服务地址,示例: `__REQ__["serverName"]` FTP服务器端口:FTP 服务端口,示例: `__REQ__["port"]` FTP用户:FTP 用户名,示例: `__REQ__["user"]` FTP密码:FTP 密码,示例: `__REQ__["password"]` 服务器文件:FTP 服务器目录中的文件,示例: `"/opt/abc.txt"` 本地目录:下载到本地哪个目录,示例: `"D:\\work\\CQYY\\workspace"`