Apache ftpserver是一款非常不错的ftp服务器,相较于vsftpd配置简单,性能也很不错,记录一下使用过程
1.下载安装包
2.解压到本地
3.修改用户信息
编辑解压目录下/res/conf/users.properties文件
#密码 配置新的用户
ftpserver.user.tzy.userpassword=123456
#主目录
ftpserver.user.tzy.homedirectory=./res/home
#当前用户可用
ftpserver.user.tzy.enableflag=true
#具有上传权限
ftpserver.user.tzy.writepermission=true
#最大登陆用户数为20
ftpserver.user.tzy.maxloginnumber=20
#同IP登陆用户数为2
ftpserver.user.tzy.maxloginperip=2
#空闲时间为300秒
ftpserver.user.tzy.idletime=300
#上传速率限制为10000000000字节每秒
ftpserver.user.tzy.uploadrate=10000000000
#下载速率限制为10000000000字节每秒
ftpserver.user.tzy.downloadrate=10000000000
需要注意的是ftpserver.user.tzy.homedirectory是设置上传文件目录的,可以自己定义
4.修改配置文件ftpd-typical.xml
<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" encrypt-passwords="clear"/>
</server>
这里可以修改监听端口,需要注意的是要去掉密码加密 encrypt-passwords="clear",管理员账号的密码是经过md5加密的
5.启动ftp服务器,命令行进入应用目录下的bin目录,输入ftpd.bat res/conf/ftpd-typical.xml,ftp服务就启动了,为了以后启动方便可以自己写个脚本
盘符:
cd ftpserverPath/bin
ftpd.bat res/conf/ftpd-typical.xml
将其保存为bat文件放在bin目录下,以后用脚本启动即可
6.代码连接ftp服务器并实现文件上传下载删除
package com.tzy.ftpstorage.utils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
public class FTPUtil {
// ftp服务器ip地址
private static String FTP_ADDRESS;
// 端口号
private static int FTP_PORT;
// 用户名
private static String FTP_USERNAME;
// 密码
private static String FTP_PASSWORD;
// 相对路径
private static String FTP_BASEPATH;
private static boolean uploadFile(String remoteFileName, InputStream input) {
System.out.println(FTP_ADDRESS);
boolean flag = false;
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8");
try {
int reply;
ftp.connect(FTP_ADDRESS, FTP_PORT);// 连接FTP服务器
ftp.login(FTP_USERNAME, FTP_PASSWORD);// 登录
reply = ftp.getReplyCode();
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//设置为被动模式
ftp.enterLocalPassiveMode();
ftp.makeDirectory(FTP_BASEPATH);
ftp.changeWorkingDirectory(FTP_BASEPATH);
//originFilePath就是上传文件的文件名,建议使用生成的唯一命名,中文命名最好做转码
boolean a = ftp.storeFile(remoteFileName, input);
// boolean a = ftp.storeFile(new String(remoteFileName.getBytes(),"iso-8859-1"),input);
System.out.println(remoteFileName);
System.out.println(a);
input.close();
ftp.logout();
flag = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return flag;
}
public static Boolean uploadFile(String remoteFileName, InputStream inputStream, String ftpAddress, int ftpPort,
String ftpName, String ftpPassWord, String ftpBasePath) {
FTP_ADDRESS = ftpAddress;
FTP_PORT = ftpPort;
FTP_USERNAME = ftpName;
FTP_PASSWORD = ftpPassWord;
FTP_BASEPATH = ftpBasePath;
uploadFile(remoteFileName,inputStream);
return true;
}
public static boolean deleteFile(String ftpAddress, int ftpPort, String ftpName, String ftpPassWord, String ftpBasePath,
String filename) {
boolean flag = false;
FTPClient ftpClient = new FTPClient();
try {
// 连接FTP服务器
ftpClient.connect(ftpAddress, ftpPort);
// 登录FTP服务器
ftpClient.login(ftpName, ftpPassWord);
// 验证FTP服务器是否登录成功
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
return flag;
}
// 切换FTP目录
ftpClient.changeWorkingDirectory(ftpBasePath);
ftpClient.dele(filename);
ftpClient.logout();
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
} catch (IOException e) {
}
}
}
return flag;
}
public static boolean downloadFile(String ftpAddress, int ftpPort, String ftpName, String ftpPassWord, String ftpBasePath,
String filename, String localPath) {
boolean flag = false;
FTPClient ftpClient = new FTPClient();
try {
// 连接FTP服务器
ftpClient.connect(ftpAddress, ftpPort);
// 登录FTP服务器
ftpClient.login(ftpName, ftpPassWord);
// 验证FTP服务器是否登录成功
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
return flag;
}
// 切换FTP目录
ftpClient.changeWorkingDirectory(ftpBasePath);
//此处为demo方法,正常应该到数据库中查询fileName
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
if (filename.equalsIgnoreCase(file.getName())) {
File localFile = new File(localPath + "/" + file.getName());
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
} catch (IOException e) {
}
}
}
return flag;
}
}
总结一下,性能还是可以的,效果令人满意。