ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 组件源码 ``` /** * <b>方法描述:</b> FTP文件上传 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2018-05-08 18:20:45 <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 localFile * 入参|本地文件|{@link java.lang.String} * @param remoteFile * 入参|上传目录文件|{@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:20:45") @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 = "localFile", comment = "本地文件", type = java.lang.String.class), @Param(name = "remoteFile", comment = "上传目录文件", type = java.lang.String.class) }) @Returns(returns = { @Return(id = "-1", desp = "异常"), @Return(id = "0", desp = "失败"), @Return(id = "1", desp = "成功") }) public static ResultBase P_ftpUpload(String ftpHost, Integer ftpPort, String ftpUser, String ftpPswd, String localFile, String remoteFile) { InputStream input = null; File fileUp = new File(localFile); if (!fileUp.exists() || !fileUp.isFile()) { return ResultBase.newFailureResult("TPTX0001", "上传文件不存在"); } try { input = new FileInputStream(fileUp); } catch (FileNotFoundException e1) { AppLog.error(e1); return ResultBase.newFailureResult("TPTX0002", "上传文件不存在"); } if (ftpPort == null || ftpPort < 1) ftpPort = 21; String[] strATmp = localFile.replaceAll("\\\\", "/").split("/"); String fileName = strATmp[strATmp.length - 1]; String svrPath = remoteFile.replaceAll("\\\\", "/"); if (!svrPath.endsWith("/")) { strATmp = svrPath.replaceAll("\\\\", "/").split("/"); fileName = strATmp[strATmp.length - 1]; svrPath = svrPath .substring(0, svrPath.length() - fileName.length()); } FTPClient ftp = new FTPClient(); try { ftp.connect(ftpHost, ftpPort); ftp.login(ftpUser, ftpPswd); int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return ResultBase.newFailureResult("TPTX0003", "登录FTP服务失败"); } if (StringUtils.isNotEmpty(svrPath) && !ftp.changeWorkingDirectory(svrPath)) { ftp.disconnect(); return ResultBase.newFailureResult("TPTX0004", "远程目录" + svrPath + "不存在"); } ftp.setFileType(FTP.BINARY_FILE_TYPE); fileName = new String(fileName.getBytes("GBK"), "iso-8859-1"); ftp.storeFile(fileName, input); input.close(); ftp.logout(); return ResultBase.newSuccessResult(); } catch (Exception e) { AppLog.error(e); return ResultBase.newExceptionResult("TPTX0005", "FTP上传文件异常:" + AppLog.errorMsg(e)); } finally { try { ftp.disconnect(); } catch (Exception ioe) { AppLog.error(ioe); } } } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/00/4c/004c6426d7669100317947581b373444_1866x899.png) # 参数说明及示例 ## 入口参数 FTP服务器地址:FTP 所在服务地址,示例: `__REQ__["serverName"]` FTP服务器端口:FTP 服务端口,示例: `__REQ__["port"]` FTP用户:FTP 用户名,示例: `__REQ__["user"]` FTP密码:FTP 密码,示例: `__REQ__["pwd"]` 本地文件:需要上传的文件,示例: `"D:\\work\\CQYY\\workspace\\abc.txt"` 上传目录文件:FTP 服务器目录,示例: `"/opt"`