使用apache的ftpserver搭建ftp服务器

吕嘉荣
2023-12-01

不用框架的话,可以用windows自带的ftp工具搭建,不过要下载i386之类的组件。apache提供了一套ftp服务器工具。下载apache-ftpserver-1.0.6.

解压到ftpserver就行了。如果不配置的话,启动bin下的ftpd.bat就行了,浏览器输入ftp://localhost:2121就行了。


打开该文件,可以看到:

rem ----- Create CLASSPATH --------------------------------------------
set FTPD_CLASSPATH=%CLASSPATH%;%FTPD_HOME%\common\classes;
cd /d "%FTPD_HOME%\common\lib"
for %%i in ("*.jar") do call "%FTPD_HOME%\bin\appendcp.bat" "%FTPD_HOME%\common\lib\%%i"
cd /d %FTPD_HOME%

这很简单吧,看看就明白了。

如果要安全和可以增加用户,那就要配置数据库了,当然,ftpserver提供了这样的功能。


apache的ftp文件目录和配置都在res下,进入可以看到了。

# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=./res/home
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0

ftpserver.user.anonymous.userpassword=
ftpserver.user.anonymous.homedirectory=./res/home
ftpserver.user.anonymous.enableflag=true
ftpserver.user.anonymous.writepermission=false
ftpserver.user.anonymous.maxloginnumber=20
ftpserver.user.anonymous.maxloginperip=2
ftpserver.user.anonymous.idletime=300
ftpserver.user.anonymous.uploadrate=4800
ftpserver.user.anonymous.downloadrate=4800

匿名用户和admin登录配置。


<?xml version="1.0" encoding="UTF-8"?>
	<!--
		Licensed to the Apache Software Foundation (ASF) under one or more
		contributor license agreements. See the NOTICE file distributed with
		this work for additional information regarding copyright ownership.
		The ASF licenses this file to you under the Apache License, Version
		2.0 (the "License"); you may not use this file except in compliance
		with the License. You may obtain a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0 Unless required by
		applicable law or agreed to in writing, software distributed under the
		License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
		CONDITIONS OF ANY KIND, either express or implied. See the License for
		the specific language governing permissions and limitations under the
		License.
	-->
<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 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd	
	   "
	id="myServer">
	<!--
		Use this section to define custom listeners, or to redefine the
		default listener, aptly named "default"
	-->
	<listeners>
		<nio-listener name="default" port="2222" implicit-ssl="true"
			idle-timeout="60" local-address="1.2.3.4">
			<ssl>
				<keystore file="mykeystore.jks" password="secret"
					key-password="otherSecret" />
				<truststore file="mytruststore.jks" password="secret" />
			</ssl>
			<data-connection idle-timeout="60">
				<active enabled="true" local-address="1.2.3.4" local-port="2323"
					ip-check="true" />
				<passive ports="123-125" address="1.2.3.4" external-address="1.2.3.4" />
			</data-connection>
			<blacklist>1.2.3.0/16, 1.2.4.0/16, 1.2.3.4</blacklist>
		</nio-listener>
	</listeners>
	<!--
		Use this section to define your Ftplets, they are configured like
		regular Spring beans
	-->
	<ftplets>
		<ftplet name="ftplet1">
			<beans:bean class="org.apache.ftpserver.examples.MyFtplet">
				<beans:property name="foo" value="123" />
			</beans:bean>
		</ftplet>
	</ftplets>
	<!-- The user manager, choose one -->
	<file-user-manager file="users.properties"
		encrypt-passwords="true" />
	<!--<db-user-manager>
		<data-source>
			<beans:bean class="some.datasoure.class" />
		</data-source>
		<insert-user>INSERT INTO FTP_USER (userid, userpassword,
			homedirectory, enableflag, writepermission, idletime, uploadrate,
			downloadrate) VALUES ('{userid}', '{userpassword}',
			'{homedirectory}',
			{enableflag}, {writepermission}, {idletime},
			{uploadrate},
			{downloadrate})
		</insert-user>
			<update-user>UPDATE FTP_USER SET
				userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}
				WHERE userid='{userid}'
		</update-user>
			<delete-user>DELETE FROM FTP_USER WHERE userid = '{userid}'
		</delete-user>
			<select-user>SELECT userid, userpassword, homedirectory,
				enableflag, writepermission, idletime, uploadrate, downloadrate,
				maxloginnumber, maxloginperip FROM
				FTP_USER WHERE userid = '{userid}'
		</select-user>
			<select-all-users>SELECT userid FROM FTP_USER ORDER BY userid
		</select-all-users>
			<is-admin>SELECT userid FROM FTP_USER WHERE userid='{userid}'
				AND
				userid='admin'
		</is-admin>
			<authenticate>SELECT userpassword from FTP_USER WHERE
				userid='{userid}'</authenticate>
	</db-user-manager> -->
	<!-- The file system -->
	<native-filesystem case-insensitive="false"
		create-home="true" />
	<!--
		Use this section to define custom commands. Custom commands can also
		override already existing commands
	-->
	<commands use-default="false">
		<command name="MYHELP">
			<beans:bean class="org.apache.ftpserver.examples.MYHELP" />
		</command>
	</commands>
	<!-- Define the available languages -->
	<messages languages="se, no ,da" />
</server>

ftpserver全配置文件。


<?xml version="1.0" encoding="UTF-8"?>
	<!--
		Licensed to the Apache Software Foundation (ASF) under one or more
		contributor license agreements. See the NOTICE file distributed with
		this work for additional information regarding copyright ownership.
		The ASF licenses this file to you under the Apache License, Version
		2.0 (the "License"); you may not use this file except in compliance
		with the License. You may obtain a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0 Unless required by
		applicable law or agreed to in writing, software distributed under the
		License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
		CONDITIONS OF ANY KIND, either express or implied. See the License for
		the specific language governing permissions and limitations under the
		License.
	-->
<server xmlns="http://mina.apache.org/ftpserver/spring/v1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	   http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd	
	   "
	id="myServer">
	<listeners>
		<nio-listener name="default" port="2121">
		    <ssl>
                <keystore file="./res/ftpserver.jks" password="password" />
            </ssl>
		</nio-listener>
	</listeners>
	<file-user-manager file="./res/conf/users.properties" />
</server>

一般配置文件。


需要哪些功能,在一般配置文件增加,或者在全配置文件内放开,修改后,要进入bin目录,执行ftpd.bat res/conf/ftpd-.xx.xml才生效。


需要新增用户,就放开数据库的配置。执行:

CREATE TABLE FTP_USER (      
   userid VARCHAR(64) NOT NULL PRIMARY KEY,       
   userpassword VARCHAR(64),      
   homedirectory VARCHAR(128) NOT NULL,             
   enableflag BOOLEAN DEFAULT TRUE,    
   writepermission BOOLEAN DEFAULT FALSE,       
   idletime INT DEFAULT 0,             
   uploadrate INT DEFAULT 0,             
   downloadrate INT DEFAULT 0,
   maxloginnumber INT DEFAULT 0,
   maxloginperip INT DEFAULT 0
);

增加一个用户,增加一个ftp目录等等。


 类似资料: