Linux 的组件(一款软件),安装到 Linux 后通过 java 代码( FtpClient )实现文件上传,VSFTPD 基于 FTP 协议
之前实现文件上传大多是直接上传各自的 tomcat 在多 tomcat 集群或分布式环境下,另一个 tomcat 却
无法获取本 tomcat 上传的文件,而且将文件上传至 tomcat 重启还存在丢失现象
使用 VSFTPD 优化后,我们统一将图片上传至 VSFTPD 服务器,然后 tomcat 都统一去同一个服务器引
用,就解决了个 tomcat 之间资源无法公用的问题
如果希望在客户端直接访问图片服务器中的图片,由于 VSFTPD 是基于 FTP 协议的,客户端浏览器是需
要通过 http 协议访问图片,所以使用 Nginx 进行反向代理(将 Nginx 和 VSFTPD 装在同一台服务器,
Nginx 直接代理那个文件上传的目录)解决这个问题。
yum -y install vsftpd
安装完成之后,配置文件存放在 /etc/vsftpd/vsftpd.conf
useradd ftpuser
此用户就是用来登录 ftp 服务器用的
这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为 /home/ftpuser;用户ftpuser应该拥有/home/ftpuser的读写权限。
passwd ftpuser
输入两次密码后修改密码
firewall-cmd --zone=public --add-port=21/tcp --permanent # 开放21端口
firewall-cmd --zone=public --remove-port=21/tcp --permanent # 关闭21端口
firewall-cmd --reload # 重起防火墙
firewall-cmd --zone=public --list-ports # 防火墙所有开放的端口
systemctl status firewalld # 查看防火墙状态
systemctl stop firewalld.service # 关闭防火墙
systemctl start firewalld.service # 开启防火墙
如果是阿里云服务器,在防火墙开启 1024-65535范围的端口,因为ftp会使用其中的一个端口来传输数据(如果不指定,那么在在浏览器上输入ftp://192.168.1.1输入账号密码后,会加载超时,链接不上(坑));
vsftp配置文件请参考 VSFTPD配置文件详解
/etc/vsftpd/ftpusers
禁止使用vsftpd的用户列表文件。记录不允许访问FTP服务器的用户名单,管理员可以把一些对系统安全有威胁的用户账号记录在此文件中,以免用户从FTP登录后获得大于上传下载操作的权利,而对系统造成损坏。(注意:linux-4中此文件在/etc/目录下)
/etc/vsftpd/user_list
禁止或允许使用vsftpd的用户列表文件。这个文件中指定的用户缺省情况(即在/etc/vsftpd/vsftpd.conf中设置userlist_deny=YES)下也不能访问FTP服务器,在设置了userlist_deny=NO时,仅允许user_list中指定的用户访问FTP服务器。(注意:linux-4中此文件在/etc/目录下)
外网是可以访问上去了,可是发现没法返回目录(使用 ftp 的主动模式,被动模式还是无法 访问),也上传
不了,因为 selinux ; 我先择直接把 selinux 关闭;
FTPClient ftp = new FTPClient();
// 设置 ip 和端口,写在用户名和密码上面
ftp.connect("192.168.139.131", 21);
//设置用户名和密码
ftp.login("ftpuser", "ftpuser");
//设置文件类型
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = new FileInputStream("E:/timg.jpg");
//第一个参数存储时名称
ftp.storeFile("abc.jpg", is);
//退出
ftp.logout();
#mple config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=6000
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=1200
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=NO
pam_service_name=vsftpd
userlist_enable=YES
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list
local_root=/home/ftp
tcp_wrappers=YES
个人学习笔记,记得比较乱,各位轻点喷;