|
| 1 | +/*package Server; |
| 2 | +
|
| 3 | +public class FTPService { |
| 4 | + FtpServerFactory serverFactory = new FtpServerFactory(); |
| 5 | + ListenerFactory factory = new ListenerFactory(); |
| 6 | +factory.setPort(1234);// set the port of the listener (choose your desired port, not 1234) |
| 7 | +serverFactory.addListener("default", factory.createListener()); |
| 8 | + PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); |
| 9 | +userManagerFactory.setFile(new File("/home/blablah/myusers.properties"));//choose any. We're telling the FTP-server where to read its user list |
| 10 | +userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() |
| 11 | + {//We store clear-text passwords in this example |
| 12 | +
|
| 13 | + @Override |
| 14 | + public String encrypt(String password) { |
| 15 | + return password; |
| 16 | + } |
| 17 | +
|
| 18 | + @Override |
| 19 | + public boolean matches(String passwordToCheck, String storedPassword) { |
| 20 | + return passwordToCheck.equals(storedPassword); |
| 21 | + } |
| 22 | + }); |
| 23 | + //Let's add a user, since our myusers.properties file is empty on our first test run |
| 24 | + BaseUser user = new BaseUser(); |
| 25 | + user.setName("test"); |
| 26 | + user.setPassword("test"); |
| 27 | + user.setHomeDirectory("/home/blablah"); |
| 28 | + List<Authority> authorities = new ArrayList<Authority>(); |
| 29 | + authorities.add(new WritePermission()); |
| 30 | + user.setAuthorities(authorities); |
| 31 | + UserManager um = userManagerFactory.createUserManager(); |
| 32 | + try |
| 33 | + { |
| 34 | + um.save(user);//Save the user to the user list on the filesystem |
| 35 | + } |
| 36 | + catch (FtpException e1) |
| 37 | + { |
| 38 | + //Deal with exception as you need |
| 39 | + } |
| 40 | + serverFactory.setUserManager(um); |
| 41 | + Map<String, Ftplet> m = new HashMap<String, Ftplet>(); |
| 42 | + m.put("miaFtplet", new Ftplet() |
| 43 | + { |
| 44 | +
|
| 45 | + @Override |
| 46 | + public void init(FtpletContext ftpletContext) throws FtpException { |
| 47 | + //System.out.println("init"); |
| 48 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 49 | + } |
| 50 | +
|
| 51 | + @Override |
| 52 | + public void destroy() { |
| 53 | + //System.out.println("destroy"); |
| 54 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 55 | + } |
| 56 | +
|
| 57 | + @Override |
| 58 | + public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException |
| 59 | + { |
| 60 | + //System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine()); |
| 61 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 62 | +
|
| 63 | + //do something |
| 64 | + return FtpletResult.DEFAULT;//...or return accordingly |
| 65 | + } |
| 66 | +
|
| 67 | + @Override |
| 68 | + public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException |
| 69 | + { |
| 70 | + //System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString()); |
| 71 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 72 | +
|
| 73 | + //do something |
| 74 | + return FtpletResult.DEFAULT;//...or return accordingly |
| 75 | + } |
| 76 | +
|
| 77 | + @Override |
| 78 | + public FtpletResult onConnect(FtpSession session) throws FtpException, IOException |
| 79 | + { |
| 80 | + //System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString()); |
| 81 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 82 | +
|
| 83 | + //do something |
| 84 | + return FtpletResult.DEFAULT;//...or return accordingly |
| 85 | + } |
| 86 | +
|
| 87 | + @Override |
| 88 | + public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException |
| 89 | + { |
| 90 | + //System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString()); |
| 91 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 92 | +
|
| 93 | + //do something |
| 94 | + return FtpletResult.DEFAULT;//...or return accordingly |
| 95 | + } |
| 96 | + }); |
| 97 | + serverFactory.setFtplets(m); |
| 98 | + //Map<String, Ftplet> mappa = serverFactory.getFtplets(); |
| 99 | + //System.out.println(mappa.size()); |
| 100 | + //System.out.println("Thread #" + Thread.currentThread().getId()); |
| 101 | + //System.out.println(mappa.toString()); |
| 102 | + FtpServer server = serverFactory.createServer(); |
| 103 | + try |
| 104 | + { |
| 105 | + server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set |
| 106 | + } |
| 107 | + catch (FtpException ex) |
| 108 | + { |
| 109 | + //Deal with exception as you need |
| 110 | + } |
| 111 | +} |
| 112 | + */ |
0 commit comments