Java 嵌入式 FTP server
jopen
12年前
FTP server的有点可靠和高效的传输数据,嵌入式FTP server的用途大家可以想想,我一般用来监控文件上传、下载事件,方便、轻量,贴出关键代码
/** * FTP Server 启动 */ public void start() { FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); // set the port of the listener factory.setPort(21); if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp server 端口21----------------------------"); } // replace the default listener serverFactory.addListener("default", factory.createListener()); //设置用户配置信息 PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); userManagerFactory.setFile(SysConfig.getInstance().getConfigFile()); if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp server 设置用户配置信息----------------------------"); } //注册FTP事件监听 Map<String, Ftplet> ftplets = new LinkedHashMap<String, Ftplet>(); ftplets.put(FtpletNotification.class.getName(), new FtpletNotification()); serverFactory.setFtplets(ftplets); if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp server 注册FTP事件监听----------------------------"); } serverFactory.setUserManager(userManagerFactory.createUserManager()); if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp server 准备启动----------------------------"); } // start the server server = serverFactory.createServer(); try { server.start(); port = serverFactory.getListener("default").getPort(); serverAddress = serverFactory.getListener("default").getServerAddress(); if(serverAddress == null) { try { serverAddress= InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } } if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp serverAddress----------------------------" + serverAddress); } if (LOG.isInfoEnabled()) { LOG.info("----------------Ftp server 启动成功----------------------------"); } } catch (FtpException e) { e.printStackTrace(); if (LOG.isErrorEnabled()) { LOG.error("FTP server 启动失败 ", e); } } }