Download: apache-ftpserver-1.1.1.zip
Only below libraries are required in this example from apache-ftpserver-1.1.1.zip
- ftpserver-core-1.1.1.jar
- ftplet-api-1.1.1.jar
- mina-core-2.0.16.jar
- slf4j-api-1.7.21.jar
- slf4j-log4j12-1.7.21.jar
- log4j-1.2.17.jar
1. Simple FTP Server Example
ftpserver will run in background after start(), and it can be stopped by stop().
FTPUserManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import java.util.ArrayList; import java.util.List; import org.apache.ftpserver.ftplet.Authentication; import org.apache.ftpserver.ftplet.AuthenticationFailedException; import org.apache.ftpserver.ftplet.Authority; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.ftplet.User; import org.apache.ftpserver.ftplet.UserManager; import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission; import org.apache.ftpserver.usermanager.impl.WritePermission; public class FTPUserManager implements UserManager { BaseUser user; public void setUser(BaseUser _user){ user = _user; if(user.getAuthorities() == null || user.getAuthorities().isEmpty()){ List<Authority> authorities = new ArrayList<Authority>(); authorities.add(new WritePermission()); authorities.add(new ConcurrentLoginPermission(10, 10)); user.setAuthorities(authorities); } } @Override public User authenticate(Authentication auth) throws AuthenticationFailedException { if(auth!=null && auth instanceof UsernamePasswordAuthentication){ UsernamePasswordAuthentication userAuth = (UsernamePasswordAuthentication) auth; if(user.getName().equals(userAuth.getUsername()) && user.getPassword().equals(userAuth.getPassword())){ return user; } } return null; } @Override public void delete(String login) throws FtpException { } @Override public boolean doesExist(String login) throws FtpException { return (user.getName().equals(login)); } @Override public String getAdminName() throws FtpException { return user.getName(); } @Override public String[] getAllUserNames() throws FtpException { return new String[]{user.getName()}; } @Override public User getUserByName(String login) throws FtpException { return user; } @Override public boolean isAdmin(String login) throws FtpException { return (user.getName().equals(login)); } @Override public void save(User login) throws FtpException { } } |
FTPServer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import org.apache.ftpserver.ConnectionConfigFactory; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.impl.BaseUser; public class FTPServer { FtpServer server; FTPUserManager userManager; int port; public FTPServer(){ port = 21; userManager = new FTPUserManager(); } public void setPort(int port){ this.port = port; } public void setUser(String login,char[] password,String home){ BaseUser user = new BaseUser(); user.setName(login); if(password !=null && password.length>0){ user.setPassword(new String(password)); } user.setHomeDirectory(home); user.setEnabled(true); userManager.setUser(user); } public void stop(){ if (server!=null && !server.isStopped()){ server.stop(); server = null; } } public void start(){ ConnectionConfigFactory configFactory = new ConnectionConfigFactory(); configFactory.setAnonymousLoginEnabled(false); configFactory.setMaxAnonymousLogins(1); configFactory.setMaxLoginFailures(5); configFactory.setLoginFailureDelay(30); configFactory.setMaxThreads(10); configFactory.setMaxLogins(10); ListenerFactory factory = new ListenerFactory(); factory.setPort(port); FtpServerFactory serverFactory = new FtpServerFactory(); serverFactory.addListener("default", factory.createListener()); serverFactory.setUserManager(userManager); serverFactory.setConnectionConfig(configFactory.createConnectionConfig()); server = serverFactory.createServer(); try{ server.start(); } catch (FtpException ex) { //TODO } } public static void main(String[] args) throws InterruptedException { FTPServer server = new FTPServer(); server.setPort(11000); server.setUser("user", "password".toCharArray(), "D:/Temp"); server.start(); System.out.println("Ftp Server Started !"); for( int i =0; i < 10; i++ ) { System.out.println("" + i); Thread.sleep(1000); } server.stop(); System.out.println("Ftp Server Stopped !"); } } |
Result:
Ftp Server Started ! 0 1 2 3 4 5 6 7 8 9 Ftp Server Stopped !
2. FTP Server Example with Callback (Ftplet)
All of the ftp command can be hooked by ftplet interface.
FTPLet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.io.IOException; import org.apache.ftpserver.ftplet.DefaultFtplet; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.ftplet.FtpRequest; import org.apache.ftpserver.ftplet.FtpSession; import org.apache.ftpserver.ftplet.FtpletResult; public class FTPLet extends DefaultFtplet { @Override public FtpletResult onConnect(FtpSession session) throws FtpException, IOException { String client = session.getClientAddress().getHostString(); System.out.println("Client " + client + " connected to FtpServer"); return super.onConnect(session); } @Override public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException { String client = session.getClientAddress().getHostString(); System.out.println("Client " + client + " disconnected from FtpServer"); return super.onDisconnect(session); } @Override public FtpletResult onLogin(FtpSession session, FtpRequest request) throws FtpException, IOException { String user = session.getUser().getName(); System.out.println("User " + user + " loged-in to FtpServer"); return super.onLogin(session, request); } } |
FTPServer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | import java.util.HashMap; import java.util.Map; import org.apache.ftpserver.ConnectionConfigFactory; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.ftplet.Ftplet; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.impl.BaseUser; public class FTPServer { FtpServer server; FTPUserManager userManager; int port; public FTPServer(){ port = 21; userManager = new FTPUserManager(); } public void setPort(int port){ this.port = port; } public void setUser(String login,char[] password,String home){ BaseUser user = new BaseUser(); user.setName(login); if(password !=null && password.length>0){ user.setPassword(new String(password)); } user.setHomeDirectory(home); user.setEnabled(true); userManager.setUser(user); } public void stop(){ if (server!=null && !server.isStopped()){ server.stop(); server = null; } } public void start(){ ConnectionConfigFactory configFactory = new ConnectionConfigFactory(); configFactory.setAnonymousLoginEnabled(false); configFactory.setMaxAnonymousLogins(1); configFactory.setMaxLoginFailures(5); configFactory.setLoginFailureDelay(30); configFactory.setMaxThreads(10); configFactory.setMaxLogins(10); ListenerFactory factory = new ListenerFactory(); factory.setPort(port); FTPLet ftplet = new FTPLet(); Map<String,Ftplet> map = new HashMap<>(); map.put("default", ftplet); FtpServerFactory serverFactory = new FtpServerFactory(); serverFactory.addListener("default", factory.createListener()); serverFactory.setUserManager(userManager); serverFactory.setConnectionConfig(configFactory.createConnectionConfig()); serverFactory.setFtplets(map); server = serverFactory.createServer(); try{ server.start(); } catch (FtpException ex) { //TODO } } public static void main(String[] args) throws InterruptedException { FTPServer server = new FTPServer(); server.setPort(11000); server.setUser("user", "password".toCharArray(), "D:/Temp"); server.start(); System.out.println("Ftp Server Started !"); for( int i =0; i < 10; i++ ) { System.out.println("" + i); Thread.sleep(1000); } server.stop(); System.out.println("Ftp Server Stopped !"); } } |
Result:
Ftp Server Started ! 0 1 Client 127.0.0.1 connected to FtpServer User user loged-in to FtpServer 2 3 4 5 Client 127.0.0.1 disconnected from FtpServer 6 7 8 9 Ftp Server Stopped !
댓글 없음:
댓글 쓰기