Groovy嵌入式Apache FTP服务器示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Groovy嵌入式Apache FTP服务器示例相关的知识,希望对你有一定的参考价值。

http://mina.apache.org/ftpserver/
  1. #!/usr/bin/env groovy
  2.  
  3. // file: FtpSvr.groovy
  4.  
  5. /*
  6. Laurence Toenjes Jan 12, 2013
  7.  
  8. Standalone Groovy ftp server script (no jars to download if Groovy is
  9. properly installed).
  10.  
  11. The default user is guest with password of guest
  12. and the ftp home/base directory is the home folder of the account
  13. running this script.
  14. A root account is also created with the ftp home/base dir set to /
  15. and the password is root (for root to fully work you would obviously
  16. have to run it under an account with expanded file permissions).
  17.  
  18. Optional command line args:
  19. arg0 defaults to port 8021.
  20. arg1 (ftp base dir) defaults to the home directory of the user running this script.
  21. */
  22.  
  23. // See http://mina.apache.org/ftpserver/ for more details.
  24.  
  25. // for OS X/*nix
  26. // don't forget to do: chmod +x FtpServer.groovy
  27. // also run as sudo ./FtpSvr.groovy for when using a port number < 1024
  28.  
  29. @Grapes([
  30. @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
  31. , @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
  32. ])
  33.  
  34. import org.apache.ftpserver.FtpServerFactory;
  35. import org.apache.ftpserver.FtpServer;
  36.  
  37. import org.apache.ftpserver.listener.ListenerFactory;
  38.  
  39. import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
  40. import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
  41. import org.apache.ftpserver.usermanager.impl.BaseUser;
  42.  
  43. import org.apache.ftpserver.ftplet.UserManager;
  44. import org.apache.ftpserver.ftplet.Authority;
  45.  
  46. import org.apache.ftpserver.usermanager.impl.WritePermission;
  47.  
  48. import java.util.Properties;
  49.  
  50. public class FtpSvr {
  51.  
  52. def go(String[] args) {
  53. String ftpUsersPropFileName = "FtpSvrUsers.properties"
  54. int defaultPort = 8021
  55.  
  56. String userHome = System.properties.'user.home';
  57. String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
  58. assert new File(homeDir).exists();
  59.  
  60. def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug
  61.  
  62. int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
  63. println "### ftp port : $port"
  64. println "### ftp homeDir: $homeDir"
  65.  
  66. // the basics
  67. // FtpServerFactory serverFactory = new FtpServerFactory();
  68. // FtpServer server = serverFactory.createServer();
  69. // server.start();
  70.  
  71. // setup users
  72.  
  73. PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
  74.  
  75. def myusersPropsFile = new File( ftpUsersPropFileName );
  76. if ( !myusersPropsFile.exists() ) {
  77. myusersPropsFile.createNewFile();
  78. }
  79.  
  80. userManagerFactory.setFile( myusersPropsFile );
  81. userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
  82.  
  83. UserManager um = userManagerFactory.createUserManager();
  84.  
  85. def users = []
  86. users << [ uid:'guest', pwd:'guest', home:homeDir ]
  87. users << [ uid:'root' , pwd:'root' , home:'/' ]
  88.  
  89. def addUser = { map ->
  90. BaseUser user = new BaseUser();
  91. user.setName( map.uid );
  92. user.setPassword( map.pwd );
  93. user.setHomeDirectory( map.home );
  94.  
  95. List<Authority> auths = new ArrayList<Authority>();
  96. Authority auth = new WritePermission();
  97. auths.add(auth);
  98. user.setAuthorities(auths);
  99. // gi user;
  100. // gi user.getAuthorities();
  101. um.save(user);
  102. }
  103.  
  104. users.each { itUserMap -> addUser( itUserMap ) }
  105.  
  106. FtpServerFactory serverFactory = new FtpServerFactory();
  107.  
  108. serverFactory.setUserManager( um );
  109.  
  110. ListenerFactory factory = new ListenerFactory();
  111.  
  112. // set the port of the listener
  113. factory.setPort( port );
  114.  
  115. // replace the default listener
  116. serverFactory.addListener("default", factory.createListener());
  117.  
  118. // start the server
  119. FtpServer server = serverFactory.createServer();
  120.  
  121. // groovy.inspect.swingui.ObjectBrowser.inspect( server );
  122. // groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );
  123. // gi server.userManager.getAllUserNames() as List;
  124.  
  125. server.start();
  126. }
  127.  
  128. public static void main(String[] args) {
  129. def mo = new FtpSvr();
  130. mo.go(args)
  131. }
  132. }

以上是关于Groovy嵌入式Apache FTP服务器示例的主要内容,如果未能解决你的问题,请参考以下文章

Groovy集合遍历 ( 使用集合的 find 方法查找集合元素 | 闭包中使用 == 作为查找匹配条件 | 闭包中使用 is 作为查找匹配条件 | 闭包使用 true 作为条件 | 代码示例 )(代

虚拟用户登录FTP错误 500 OOPS: bad bool value in config file for: anon_world_readable_only Login failed.(示例代

Groovy循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )(代

apache开源java ftp文件服务器mina ftpserver安装部署,Windows

ideMyBatis报错: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):(示例代

Error creating bean with name 'org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration(示例代