Apache FtpServer是当下最热门的走ftp协议的用于用户上传下载的服务器。
 
一般来说,用的话,去官网下载完整的项目文件ftpserver-1.0.6.zip【windows版】和ftpserver-1.0.6.tar.gz【linux版】到本地,解压之后配置一下\ftpserver-1.0.6\apache-ftpserver-1.0.6\res\conf\下的users.properties或者ftpd-full.xml【主要看你走什么样的用户验证方式。users.properties:把用户信息配置在这个文件中。ftpd-full.xml:把用户信息配置在数据库中】
这样去bin目录下启动程序就好了。
 
但是Apache FtpServer从官方文档来看,都没有提及如何改变其中一些功能,只是解释一些它提供的标准功能。
这样对于企业级的应用来说,有些功能并不能满足现有的业务。
 
比如,我想限制每个ftp用户上传文件到他的文件目录下时,我想限制每个用户最多上传文件的总大小不超过50M。因为我不想被恶意用户弄爆我的服务器。我查阅了大量资料,发现标准的官方版里面是没有这样的功能的,在ftpd-full.xml和users.properties这里面也没有任何可以修改的参数来控制这样的业务。最后我决定自己通过编程来实现这样一个专门服务于我的这样的FtpServer。
我有这种想法的原因是,Apache FtpServer是纯java写的服务,而且提供了丰富的java接口。
 
查阅了大量资料后,还是找不到怎么用代码从jar里面启动整个server。
最后用反编译工具反编译整个Apache FtpServer项目后发现了,找到了启动的入口。
 public MyFtpServer() throws FtpException{

         //读取my-ftpd-full.xml,连接数据库和监控配置,然后来启动server
FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(xmlPath);
FtpServer server = null;
if (ctx.containsBean("server")) {
server = (FtpServer)ctx.getBean("server");
} else {
String[] beanNames = ctx.getBeanNamesForType(FtpServer.class);
if (beanNames.length == 1) {
server = (FtpServer)ctx.getBean(beanNames[0]);
} else if (beanNames.length > 1) {
logger.info("Using the first server defined in the configuration, named " + beanNames[0]);
server = (FtpServer)ctx.getBean(beanNames[0]);
} else {
logger.info("XML configuration does not contain a server configuration");
}
} //ftp服务器启动
server.start(); //在jvm关闭的时候,清理函数
addShutdownHook(server);
} /**
* 清理的垃圾的钩子函数
* @param engine
*/
private void addShutdownHook(final FtpServer engine)
{
Runnable shutdownHook = new Runnable() {
public void run() {
logger.info("Stopping server...");
engine.stop();
}
};
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new Thread(shutdownHook));
}
配合上我的研究发现,提供的接口中可以在ftpd-full.xml中配置
 
 
    <ftplets>
<ftplet name="MyFtplet">
<beans:bean class="com.shiyi.km.ftpserver.control.MyFtplet">
<!-- <beans:property name="foo" value="123" /> -->
</beans:bean>
</ftplet>
</ftplets>
类似于监听器的东西,可以监听每个用户在上传或者下载的动作,在这些监听事件里面,我每次都去计算那个用户目录下面的文件size总大小,如果超过限制,就stop,并返回警告代码和消息给用户。

 import java.io.File;
import java.io.IOException; import org.apache.ftpserver.ftplet.DefaultFtpReply;
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;
import org.apache.ftpserver.ftplet.User;
import org.apache.log4j.Logger; import com.shiyi.km.ftpserver.util.ConfigUtil;
import com.shiyi.km.ftpserver.util.FileUtil; /**
*
* @fileName MyFtplet.java
* @author chenkaideng
* @date 2015年8月11日
* @describe 监控事件
*/
public class MyFtplet extends DefaultFtplet{
private static final Logger logger = Logger.getLogger(MyFtplet.class); @Override
public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
throws FtpException, IOException {
//获取用户信息
User user = session.getUser();
logger.info(String.format("用户信息,用户名【%s】,用户工作目录【%s】", user.getName(),user.getHomeDirectory()));
File file=new File(user.getHomeDirectory()); //判断传入对象是否为一个文件夹对象
if(!file.isDirectory()){
logger.info("用户的HomeDirectory不是一个文件夹,请检查路径是否有误!!");
}
else{
ConfigUtil configUtil = ConfigUtil.getInstance();
if(FileUtil.overSizeLimit(configUtil.getQuota(), file)){
logger.error(String.format("目前用户[%s]目录下的文件总大小超过配额!!!", user.getName()));
session.write(new DefaultFtpReply(228, "The files under the directory over quota"));
return FtpletResult.DISCONNECT;
}
}
return super.onUploadStart(session, request);
}
}
差不多整体的思路是这样的。
 
 
有了这些基础,还可以实现各种各样的不同需求,来管理用户目录或者一些其他的业务。

Apache FtpServer扩展【动手实现自己的业务】的更多相关文章

  1. springboot整合apache ftpserver详细教程(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...

  2. 使用Apache FtpServer搭建FTP服务器 [FlashFXP]

    <server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...

  3. 01.Apache FtpServer配置

    1.解压Apache FTPServer 将下载下来的压缩包(ftpserver-1.0.6.zip)解压到本地,其目录结构如下图: 2.修改users.properties 修改 \apache-f ...

  4. apache FtpServer 整合spring部署

    我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...

  5. apache FtpServer整合spring逻辑处理

    上面我们的部署工作完成了,那么文件上传下载后,ftpserver会自动相应我们的上传下载操作,也就是说ftpServer服务器会得到触发,那么我们如果要得到文件的一些信息,比如说文件的路径.大小.类型 ...

  6. Apache FtpServer 实现文件的上传和下载

    1 下载需要的jar包 Ftp服务器实现文件的上传和下载,主要依赖jar包为: 2 搭建ftp服务器 参考Windows 上搭建Apache FtpServer,搭建ftp服务器 3 主要代码 在ec ...

  7. (转载)Windows 上搭建Apache FtpServer

    因工作需要,最近经常接触到FTP,今天我来介绍一个开源的FTP服务器,那就是Apache FTPServer,Apache FTPServer是一个100%纯Java的FTP服务器. 它的设计是基于现 ...

  8. Apache和Tomcat整合(一个Apache 不同域名处理多个不同业务)

    一.简介 在项目中,几乎任何一个项目都包括静态资源和动态请求两大部分.特别对于门户网站这样的项目,静态内容资源会更多,我们使用一般的 Tomcat 部署时,Tomcat 对静态资源的处理能力比较慢,至 ...

  9. android学习:Android上面部署Apache FTPServer

    经过了几天的研究,终于Apache FTPServer在Android的配置和使用上有了一些心得,现在分享出来,提供给大家参考,说到这儿又不得不吐槽一下这要命的转载了,找Apache FTPServe ...

随机推荐

  1. Redis系统管理相关指令简介

    常用命令列表 DBSIZE                                            返回当前数据库 Key 的数量 INFO                       ...

  2. Learning Puppet — Resource Ordering

    Learning Puppet — Resource Ordering Learn about dependencies and refresh events, manage the relation ...

  3. 重复ID的记录,只显示其中1条

    --重复ID的记录,只显示其中1条 --生成原始表 select * into #tempTable from ( select '1' as id ,'a' as name union all se ...

  4. Thinkpad X240修改bios引导方式

    来源:http://blog.csdn.net/jsship/article/details/19121149 修改笔记本的BIOS设置!这是非常重要的步骤之一.否则,你的U盘不能引导手提电脑进入PE ...

  5. 【mybatis】之批量添加

    mybatis批量添加xml <insert id="batchCreate"> INSERT INTO `roomer` (`order`,name,idCard,m ...

  6. ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)

    一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...

  7. (WF)

    Caught: System.InvalidOperationException: The argument of type 'XXX' cannot be used. Make sure that ...

  8. [jQuery].scrollTop() 函数详解

    scrollTop()函数用于设置或返回当前匹配元素相对于垂直滚动条顶部的偏移. 当一个元素的实际高度超过其显示区域的高度时,在一定的设置下,浏览器会为该元素显示相应的垂直滚动条.此时,scrollT ...

  9. room-views-用窗口颜色清除背景(Clear Background with Window Colour)选项

    这个选项是默认开启的,它的作用是在游戏每一帧绘制以前,都用一个颜色打底(覆盖整个游戏场景包括背景,从而实现背景清除),然后在这个基础上再画背景.场景等等. 如果关闭,则在游戏每一帧以前绘制背景(绘制背 ...

  10. linux下安装mysql数据库与相关操作

    如下命令都是用root身份安装,或者在命令前加上sudo 采用yum安装方式安装 yum install mysql #安装mysql客户端 yum install mysql-server #安装m ...