🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 组件源码 ``` /** * <b>方法描述:</b> SFTP文件上传 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2018-05-08 18:24:17 <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 localFile * 入参|本地文件|{@link java.lang.String} * @param remoteFile * 入参|上传目录文件|{@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:24:17") @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 = "localFile", comment = "本地文件", type = java.lang.String.class), @Param(name = "remoteFile", comment = "上传目录文件", type = java.lang.String.class) }) @Returns(returns = { @Return(id = "-1", desp = "异常"), @Return(id = "1", desp = "成功") }) public static ResultBase P_sftpUpload(String host, Integer port, String username, String password, String localFile, String remoteFile) { ChannelSftp sftp = null; Session session = null; 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()); } InputStream is = null; try { is = new FileInputStream(new File(localFile)); } catch (Exception ex) { return ResultBase.newExceptionResult("TPTX0015", "打开本地文件" + localFile + "失败"); } 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服务器失败"); } try { sftp.cd(svrPath); } catch (Exception e) { String[] dirs = svrPath.split("/"); String tempPath = ""; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; try { sftp.cd(tempPath); } catch (Exception ex) { AppLog.error(e); sftp.mkdir(tempPath); sftp.cd(tempPath); } } } sftp.put(is, fileName); return ResultBase.newSuccessResult(); } catch (Exception ex) { AppLog.error(ex); return ResultBase.newExceptionResult("TPTX0016", "SFTP文件上传异常:"+AppLog.errorMsg(ex)); } finally { if (sftp != null && sftp.isConnected()) { sftp.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/af/b9/afb927a9f9288421853fa4b39ded7e0e_1870x908.png) # 参数说明及示例 ## 入口参数 服务器地址:SFTP 所在服务地址,示例: `__REQ__["serverName"]` 服务器端口:SFTP 服务端口,示例: `__REQ__["port"]` 用户:SFTP 用户名,示例: `__REQ__["user"]` 密码:SFTP 密码,示例: `__REQ__["password"]` 本地文件:需要上传的文件,示例: `"D:\\work\\CQYY\\workspace\\abc.txt"` 上传目录文件:SFTP 服务器目录,示例: `"/opt"`