利用apache ftpserver搭建ftp服务器的方法步骤

操作环境:

  • win2012r2 x64 datacenter
  • Apache FtpServer 1.2.0
  • Java SE Development Kit 8u333
  • commons-dbcp2-2.9.0.jar
  • commons-pool2-2.11.1.jar
  • mysql server 8.0.29
  • mysql-connector-java-8.0.29.jar
  • sqlite
  • sqlite-jdbc-3.36.0.3.jar

如下图:

利用apache ftpserver搭建ftp服务器的方法步骤_第1张图片

一、usermanager采用文件形式管理xml示例如下


  

  
    
        
                
            
    
  
  

二、usermanager采用mysql数据库管理用户时,ftpd-mysql.xml示例如下

目前数据库管理用户时采用的明文存储,salted和md5的方式没有测试成功,如有测试成功的朋友请指导一下。




    
        
            
                
            
        
    
    
        
            
                
                
                
                
            
        
        INSERT INTO FTP_USER (userid, userpassword,
            homedirectory, enableflag, writepermission, idletime, uploadrate,
            downloadrate) VALUES ('{userid}', '{userpassword}',
            '{homedirectory}',
            {enableflag}, {writepermission}, {idletime},
            {uploadrate},
            {downloadrate})
        
        UPDATE FTP_USER SET
            userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}
            WHERE userid='{userid}'
        
        DELETE FROM FTP_USER WHERE userid = '{userid}'
        
        SELECT userid, userpassword, homedirectory,
            enableflag, writepermission, idletime, uploadrate, downloadrate,
            maxloginnumber, maxloginperip FROM
            FTP_USER WHERE userid = '{userid}'
        
        
            SELECT userid FROM FTP_USER ORDER BY userid
        
        SELECT userid FROM FTP_USER WHERE userid='{userid}'
            AND
            userid='admin'
        
        SELECT userpassword from FTP_USER WHERE
            userid='{userid}'
        
    

注意:org.apache.commons.dbcp2.BasicDataSource,看最新的commons.dbcp命名空间和1.x版本有区别

三、usermanager采用Sqlite数据库管理用户时,ftpd-sqlite.xml示例如下




    
        
            
                
            
        
    
    
        
            
                
                
            
        
        INSERT INTO FTP_USER (userid, userpassword,
            homedirectory, enableflag, writepermission, idletime, uploadrate,
            downloadrate) VALUES ('{userid}', '{userpassword}',
            '{homedirectory}',
            {enableflag}, {writepermission}, {idletime},
            {uploadrate},
            {downloadrate})
        
        UPDATE FTP_USER SET
            userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}
            WHERE userid='{userid}'
        
        DELETE FROM FTP_USER WHERE userid = '{userid}'
        
        SELECT userid, userpassword, homedirectory,
            enableflag, writepermission, idletime, uploadrate, downloadrate,
            maxloginnumber, maxloginperip FROM
            FTP_USER WHERE userid = '{userid}'
        
        
            SELECT userid FROM FTP_USER ORDER BY userid
        
        SELECT userid FROM FTP_USER WHERE userid='{userid}'
            AND
            userid='admin'
        
        SELECT userpassword from FTP_USER WHERE
            userid='{userid}'
        
    

注意:commons的jar包还保留着,多了个操作sqlitejdbc的jar包,下载地址:GitHub - xerial/sqlite-jdbc: SQLite JDBC Driver

四、解决ftpd.exe在64位windows系统启动失败的问题

需下载tomcat包,目前测试的这个版本可行tomcat-7 v7.0.109 (apache.org)

利用apache ftpserver搭建ftp服务器的方法步骤_第2张图片

放入apache ftpserver bin目录里替换原有的ftpd.exe

这样安装为服务的时候就可以正常启动了

五、python操作sqlite的ftp.db管理(增加删除)用户

自己搞了个python脚本,采用了sqlalchemy来操作数据库

from sqlalchemy import create_engine
from sqlalchemy import MetaData,Table,Column,Boolean,Integer,String
import os

engine=create_engine('sqlite:///ftp.db')
conn=engine.connect()

metadata=MetaData()

ftpusers=Table('FTP_USER',metadata,
    Column('userid',String(64),primary_key=True),
    Column('userpassword',String(64),nullable=False),
    Column('homedirectory',String(128),nullable=False),
    Column('enableflag',Boolean(),default=True),
    Column('writepermission',Boolean(),default=True),
    Column('idletime',Integer(),default=0),
    Column('uploadrate',Integer(),default=0),
    Column('downloadrate',Integer(),default=0),
    Column('maxloginnumber',Integer(),default=0),
    Column('maxloginperip',Integer(),default=0)
)

metadata.create_all(engine)

def addgeneraluser():

	deluser = ftpusers.delete().where(ftpusers.c.userid=="nic")
	rs = conn.execute(deluser)

	dirname="./files/alluser"
	if not os.path.exists(dirname):
		os.mkdir(dirname)

	ins=ftpusers.insert().values(
		userid="nic",
		userpassword="123321",
		homedirectory=dirname,
		writepermission=0,
		maxloginnumber=1
	)

	result=conn.execute(ins)

def addadmin():
	deladmin = ftpusers.delete().where(ftpusers.c.userid=="admin")
	rs = conn.execute(deladmin)

	ins=ftpusers.insert().values(
		userid="admin",
		userpassword="123456",
		homedirectory="./files",
		writepermission=1
	)

	result=conn.execute(ins)

def getusers():
	sel=ftpusers.select()
	rs=conn.execute(sel)
	print(rs.fetchall())
	
addgeneraluser()
getusers()

可以方便的增加用户了,generaluser只读权限只能同时登录一个,admin权限可读写,不限制。

到此这篇关于利用apache ftpserver搭建ftp服务器的方法步骤的文章就介绍到这了,更多相关apache ftpserver搭建ftp内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(利用apache ftpserver搭建ftp服务器的方法步骤)