|
| 1 | +package Server.FTPServer; |
| 2 | + |
| 3 | +import org.apache.ftpserver.FtpServer; |
| 4 | +import org.apache.ftpserver.FtpServerFactory; |
| 5 | +import org.apache.ftpserver.ftplet.*; |
| 6 | +import org.apache.ftpserver.listener.ListenerFactory; |
| 7 | +import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; |
| 8 | +import org.apache.ftpserver.usermanager.impl.BaseUser; |
| 9 | +import org.apache.ftpserver.usermanager.impl.WritePermission; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +public class FTPService { |
| 19 | + public void ftpServer() { |
| 20 | + //setting up a server Factory |
| 21 | + FtpServerFactory serverFactory = new FtpServerFactory(); |
| 22 | + ListenerFactory factory = new ListenerFactory(); |
| 23 | + |
| 24 | + factory.setPort(5000);// set the port of the listener (choose your desired port, not 1234) |
| 25 | + serverFactory.addListener("default", factory.createListener()); // adding a "default Listener" |
| 26 | + PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass |
| 27 | + userManagerFactory.setFile(new File("~/ftp/userList.properties"));//choose any. We're telling the FTP-server where to read its user list |
| 28 | + userManagerFactory.setPasswordEncryptor(new PasswordEncryptorsImpl()); // encrypts passwords of users by using the EncrImpl |
| 29 | + |
| 30 | + //Let's add a user, since our userList.properties file is empty on our first test run |
| 31 | + BaseUser user = new BaseUser(); |
| 32 | + |
| 33 | + user.setName("ohdmOffViewer"); |
| 34 | + user.setPassword("H!3r0glyph Sat3llite Era$er"); |
| 35 | + user.setHomeDirectory("/map"); |
| 36 | + |
| 37 | + List<Authority> authorities = new ArrayList<>(); |
| 38 | + authorities.add(new WritePermission()); |
| 39 | + user.setAuthorities(authorities); |
| 40 | + UserManager um = userManagerFactory.createUserManager(); |
| 41 | + |
| 42 | + try { |
| 43 | + um.save(user);//Save the user to the user list on the filesystem |
| 44 | + } catch (FtpException e1) { |
| 45 | + //Deal with exception as you need |
| 46 | + } |
| 47 | + |
| 48 | + serverFactory.setUserManager(um); |
| 49 | + Map<String, Ftplet> m = new HashMap<>(); |
| 50 | + m.put("miaFtplet", new FtpLetImpl()); |
| 51 | + |
| 52 | + serverFactory.setFtplets(m); |
| 53 | + |
| 54 | + //Map<String, Ftplet> mappa = serverFactory.getFtplets(); |
| 55 | + //System.out.println(mappa.size()); |
| 56 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 57 | + //System.out.println(mappa.toString()); |
| 58 | + |
| 59 | + FtpServer server = serverFactory.createServer(); |
| 60 | + try { |
| 61 | + server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set |
| 62 | + } catch (FtpException ex) { |
| 63 | + //Deal with exception as you need |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments