FTP服务搭建
1、检查是否已经安装了vsftp
首先查看系统中是否成功安装了vsftpd,执行下面命令(有内容则安装了ftp)
rpm -qa | grep vsftpd
2、开始安装
yum install vsftpd -y
3、启动FTP服务
systemctl start vsftpd.service
配置
进入cd /etc/vsftpd目录
ftp的配置文件主要有三个,位于/etc/vsftpd/目录下,分别是:
ftpusers 该文件用来指定那些用户不能访问ftp服务器。
user_list 该文件用来指示的默认账户在默认情况下也不能访问ftp
vsftpd.conf vsftpd的主配置文件
ftp配置:vi /etc/vsftpd/vsftpd.conf
#禁止匿名登录FTP服务器
anonymous_enable=NO
#是否允许本地用户登录
local_enable=YES
#是否允许写入
write_enable=YES
#文件夹的默认权限为022
local_umask=022
#进入每个目录是否显示欢迎信息
dirmessage_enable=YES
#上传/下载文件时记录日志
xferlog_enable=YES
#主动模式(PORT)才有效,是否使用20端口传输数据
connect_from_port_20=YES
#使用标准文件日志
xferlog_std_format=YES
#开启ipv4监听,与listen_ipv6不能同时开启
listen=NO
#同时监听IPv4和IPv6的FTP请求
listen_ipv6=YES
#使用pam模块控制
pam_service_name=vsftpd
#控制用户访问,/etc/vsftpd/user_list是一个黑名单
userlist_enable=YES
#控制主机访问,检查/etc/hosts.allow 和/etc/hosts.deny 中的设置,来决定请求连接的主机,是否允许访问该FTP服务器。
tcp_wrappers=YES
默认被动模式
PORT主动模式:pasv_enable=NO
PASV被动模式:pasv_enable=YES
创建ftp用户
这里主要需要创建的用户无法使用普通登录功能或者telnet,只允许使用ftp登录
useradd -s /sbin/nologin -d /home/ftpuser ftpuser
为用户设置密码
passwd ftpuser
530 Login incorrect.:
注释 vi /etc/pam.d/vsftpd 文件里的这一行,#auth required pam_shells.so
这个语句的意思是只有包含shell的用户才能登录,但是我们创建的用户是nologin不允许登陆的
6、防火墙设置
# 方法一:关闭防火墙
systemctl stop firewalld
# 方法二:开放端口
# 提示:防火墙iptables是centos7之前的服务,centos7开始是firewalld服务,开放端口流程请自行百度。另外注意云服务器需要在云平台上设置安全组开放端口
开启
# iptables -I INPUT -i eth0 -p tcp --dport 20 -j ACCEPT
# iptables -I OUTPUT -o eth0 -p tcp --sport 20 -j ACCEPT
保存
# /etc/rc.d/init.d/iptables save
重启防火墙
# service iptables restart
7、修改selinux,将SELINUX改为disabled
SELINUX=disabled
8、设置开机启动
chkconfig vsftpd on
或者
systemctl enable vsftpd.service
9、重启FTP服务
systemctl restart vsftpd.service
其他命令
#开启
service vsftpd start
#停止
service vsftpd stop
#重启
service vsftpd restart
测试代码
package com.transfer.util;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Ftp工具
* @date 2019年12月6日 上午8:56:44
*/
public class FtpUtil {
private static final Logger logger = LoggerFactory.getLogger(FtpUtil.class);
private FTPClient ftpClient = null;
public static void main(String[] args) {
boolean flag;
String ip = "192.168.111.201";
int port = 21;
String username = "ftpuser";
String password = "123456";
FtpUtil ftpUtil = new FtpUtil();
ftpUtil.login(ip, port, username, password);
//上传文件
String remotePath = "/a/b/c";
String fileName = "测试2.txt";
String localPath = "F:/测试文件.txt";
//flag= ftpUtil.uploadFile(remotePath, fileName, localPath);
// System.out.println(flag);
//下载文件
remotePath = "/a/b/c";
fileName = "测试2.txt";
localPath = "F:/测试文件-下载.txt";
// flag = ftpUtil.downloadFile(remotePath, fileName, localPath);
// System.out.println(flag);
//删除文件
remotePath = "/a/b/c";
fileName = "测试2.txt";
flag = ftpUtil.deleteFile(remotePath, fileName);
System.out.println(flag);
}
/**
* 登陆
* @param ip
* @param port
* @param username
* @param password
* @date 2019年12月6日 上午9:05:11
*/
public boolean login(String ip, int port, String username, String password) {
ftpClient = new FTPClient();
try {
logger.info("connecting...ftp服务器:" + ip + ":" + port);
ftpClient.setControlEncoding("UTF-8");// 编码设置必须放在connect前不然不起作用
ftpClient.setConnectTimeout(30000);//建立 Socket 连接过程中的超时处理,毫秒
ftpClient.setDefaultTimeout(30000);//传输控制命令的 Socket 的 SoTimeout 的超时处理,毫秒
ftpClient.setDataTimeout(30000);//传输数据的 Socket 的 SoTimeout 的超时处理,毫秒
ftpClient.connect(ip, port); // 连接ftp服务器
ftpClient.login(username, password); // 登录ftp服务器
ftpClient.setRemoteVerificationEnabled(false);//取消服务器获取自身Ip地址和提交的host进行匹配,否则当不一致时报出以上异常。
ftpClient.enterLocalPassiveMode();//被动模式,PASV模式。通知服务器打开一个数据端口,客户端将连接到这个端口进行数据传输。
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024);
int replyCode = ftpClient.getReplyCode(); // 是否成功登录服务器
if (FTPReply.isPositiveCompletion(replyCode)) {
logger.info("连接成功...ftp服务器:" + ip + ":" + port);
return true;
}
logger.info("连接失败...ftp服务器:" + ip + ":" + port);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
return false;
}
/**
* 上传文件
* @param remotePath ftp服务文件路径
* @param fileName ftp服务要保存的文件名
* @param localPath 本地文件绝对路径
* @return
*/
public boolean uploadFile(String remotePath, String fileName, String localPath) {
FileInputStream fis = null;
boolean flag = false;
try {
logger.info("开始上传文件");
fis = new FileInputStream(localPath);
createDirecroty(remotePath);//ftp创建目录
flag = ftpClient.storeFile(fileName, fis);
ftpClient.logout();
} catch (Exception e) {
logger.error("上传文件失败");
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
/**
* 下载文件
* @param remotePath ftp服务文件路径
* @param fileName ftp服务保存的文件名
* @param localPath 本地文件绝对路径
* @return
*/
public boolean downloadFile(String remotePath, String fileName, String localPath) {
FileOutputStream fos = null;
boolean flag = false;
try {
logger.info("开始下载文件");
//由于默认登录后是进入/目录(注意这里的/实际是指:/home/用户名/)
if (remotePath.charAt(0) == '/') {
remotePath = remotePath.substring(1);
}
// FTPFile[] ftpFiles = ftpClient.listFiles(remotePath);
// String[] listNames = ftpClient.listNames(remotePath);
// 切换FTP目录
ftpClient.changeWorkingDirectory(remotePath);
File localFile = new File(localPath);
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
fos = new FileOutputStream(localFile);
//第一个参数: fileName 如果以/开头就是绝对路径,否则是相对路径
flag = ftpClient.retrieveFile(fileName, fos);
ftpClient.logout();
} catch (Exception e) {
logger.error("下载文件失败");
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
/**
* ftp创建目录
* 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
* @date 2020/9/1 15:42
*/
public boolean createDirecroty(String remote) {
try {
String directory = remote;
if (!remote.endsWith("/")) {
directory = directory + "/";
}
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equals("/") && !ftpClient.changeWorkingDirectory(directory)) {
int start = 0, end;
if (directory.startsWith("/")) {
start = 1;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = remote.substring(start, end);
// 目录不存在就创建
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 删除文件
* @param remotePath ftp服务文件路径
* @param fileName 要删除的文件名称
* @return
*/
public boolean deleteFile(String remotePath, String fileName) {
boolean flag = false;
try {
logger.info("开始删除文件");
//由于默认登录后是进入/目录(注意这里的/实际是指:/home/用户名/)
if (remotePath.charAt(0) == '/') {
remotePath = remotePath.substring(1);
}
// 切换FTP目录
ftpClient.changeWorkingDirectory(remotePath);
int dele = ftpClient.dele(fileName);
ftpClient.logout();
if (dele == 250) {
flag = true;
}
} catch (Exception e) {
logger.error("删除文件失败");
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
还没有评论,来说两句吧...