摘要
目前各种各样的所谓国产自研层出不穷,但是魔改的又是较老或者是较新的Linux内核,就导致一些软件不好使。于是简单用Java基于Apache FTPServer简单实现了一款。
正文
三种方式
- Linux版vsftpd:参考那些年,我玩过的bash脚本
- Windows版FTP
- Java版FTP
前两款都是已有的软件,第三款是根据开源进一步进行了定制化。
生产环境,建议使用vsftpd,这个对高并发支持比较友好。
我在生产环境中,简单对比了下vsftpd和apache mina ftpserver读写速度,均开通1000个被动传输端口,不限制用户登录数和速率,针对mina ftpserver还设置了不限制线程。放下最后的对比,前者的吞吐量约400/s,后者吞吐量约30/s。
一、Windows版FTP
1.) win+R > 在运行窗口输入control 回车;
2.) 控制面板-程序-启用或者关闭Windows功能,按照如图勾选。
3.) 按win键,输入IIS,打开。右键添加FTP站点,进行配置即可。
二、Java版FTP
2.1 meethigher/ftp-server
ftp-server 是基于Apache FtpServer 1.2.0 Release — Apache MINA用 Java 语言实现的 FTP 服务。它非常轻巧且易于使用。
下载release,解压后直接运行。
1
| java -jar ftp-server.jar
|
通过 server.properties 来配置FTP服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| port=21
idleSeconds=60
activeLocalPort=20
passivePorts=30000-30005
anonymousLoginEnabled=true
loginFailureDelay=500
maxLogins=1000
maxAnonymousLogins=2
maxLoginFailures=3
maxThreads=0
web.enable=false
web.port=8080
web.username=admin
web.password=admin
|
通过新增/删除/修改 users 文件夹下的 .properties 文件,来管理用户
1
2
3
4
5
6
7
8
9
10
11
12
| enabled=true
name=ftpadmin
password=ftpadmin
homeDir=C:/Users/meethigher/Desktop
# indicates the file path with write permission. if it is /ccc-test, it means that there is write permission under the ccc-test folder.
write=/ccc-test
maxConcurrentLogins=5
maxConcurrentLoginsPerIP=1
# bytes/second
maxDownloadRate=1024000
# bytes/second
maxUploadRate=1024000
|
Windows配置服务
参照Windows系统使用技巧中的windows服务管理。
将WinSW.exe命名为ftp-server.exe
创建ftp-server.xml
1
2
3
4
5
6
7
8
9
| <service>
<id>FTP-Server</id>
<name>FTP-Server</name>
<description>开源免费、轻量易用的FTP服务器,支持网络限速、用户授权、日志审计等功能</description>
<executable>D:\Develop\Java\jdk1.8.0_361\bin\java.exe</executable>
<arguments> -jar ftp-server.jar</arguments>
<log mode="reset"/>
<logpath>service-logs</logpath>
</service>
|
执行命令安装/卸载服务
1
| ftp-server.exe install|uninstall
|
linux配置服务
Linux配置服务参考Linux配置Jar包服务实现自启动
创建Linux服务
1
2
3
4
5
6
7
8
9
10
11
12
| cat >/etc/systemd/system/ftpserver.service<<EOF
[Unit]
Description=ftp-server
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/sh -c "java -jar ftp-server.jar"
WorkingDirectory=/root/tftpserver
[Install]
WantedBy=multi-user.target
EOF
|
之后就可以进行操作啦
1
2
| systemctl daemon-reload
systemctl start|stop|restart|enable|disable ftpserver
|
2.2 apache/ftpserver
如果使用的是ApacheFTPServer的话,在ftpserver根目录下创建res/conf/conf.xml
如果不想指定ip的话,将所有的跟ip相关的属性删除即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <?xml version="1.0" encoding="UTF-8"?>
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://mina.apache.org/ftpserver/spring/v1 https://mina.apache.org/ftpserver-project/ftpserver-1.0.xsd"
id="FTPServer"
max-logins="2000"
anon-enabled="false"
max-anon-logins="1"
login-failure-delay="500"
max-login-failures="3"
max-threads="0">
<listeners>
<nio-listener name="default" port="21" implicit-ssl="false"
idle-timeout="60" local-address="10.0.0.1">
<data-connection idle-timeout="60">
<active enabled="true" local-address="10.0.0.1" local-port="20" ip-check="true"/>
<passive ports="123-125" address="10.0.0.1" external-address="10.0.0.1"/>
</data-connection>
<blacklist>1.2.3.0/16, 1.2.4.0/16, 1.2.3.4</blacklist>
</nio-listener>
</listeners>
<file-user-manager file="res/conf/users.properties" encrypt-passwords="false"/>
<native-filesystem case-insensitive="false" create-home="true"/>
</server>
|
若是在离线环境中,则需要将xml约束由互联网环境迁移至本地环境。相关知识可以参考XML可扩展标记语言 - 言成言成啊
示例如下
1
2
3
4
5
6
7
| <?xml version="1.0" encoding="UTF-8"?>
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
classpath:res/conf/spring-beans-2.5.xsd http://mina.apache.org/ftpserver/spring/v1
classpath:res/conf/ftpserver-1.0.xsd">
</server>
|
目录如下
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
| ├─bin
│ appendcp.bat
│ ftpd.bat
│ ftpd.exe
│ ftpd.sh
│ ftpdw.exe
│ readme.txt
│ service.bat
│
├─common
│ ├─classes
│ │ log4j.properties
│ │
│ └─lib
│ aopalliance-1.0.jar
│ ftplet-api-1.2.0.jar
│ ftpserver-core-1.2.0.jar
│ jcl-over-slf4j-1.7.36.jar
│ log4j-api-2.17.2.jar
│ log4j-core-2.17.2.jar
│ mina-core-2.1.6.jar
│ README.txt
│ reload4j-1.2.19.jar
│ slf4j-api-1.7.36.jar
│ slf4j-reload4j-1.7.36.jar
│ spring-beans-2.5.5.jar
│ spring-context-2.5.5.jar
│ spring-core-2.5.5.jar
│
└─res
│ ftp-db.sql
│ ftpserver.jks
│
├─conf
│ conf.xml
│ ftpd-full.xml
│ ftpd-typical.xml
│ ftpserver-1.0.xsd
│ README.txt
│ spring-beans-2.5.xsd
│ users.properties
│
├─home
│ README.txt
│
└─log
ftpd.log
README.txt
|
在程序根路径下,启动程序
1
| sh bin/ftpd.sh res/conf/conf.xml
|
配置成Linux服务
1
2
3
4
5
6
7
8
9
10
11
12
| cat >/etc/systemd/system/ftpserver.service<<EOF
[Unit]
Description=ftp-server
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/sh bin/ftpd.sh res/conf/conf.xml
WorkingDirectory=/root/tftpserver
[Install]
WantedBy=multi-user.target
EOF
|