服务器
vsftpd 是“very secure ftp daemon”的缩写,安全性是它的一个最大的特点。
vsftpd 是一个 unix 类操作系统上运行的服务器的名字,它可以运行在诸如 linux、bsd、solaris、 hp-unix等系统上面,是一个完全免费的、开放源代码的ftp服务器软件,支持很多其他的 ftp 服务器所不支持的特征。
比如:非常高的安全性需求、带宽限制、良好的可伸缩性、可创建虚拟用户、支持ipv6、速率高等。
vsftpd是一款在linux发行版中最受推崇的ftp服务器程序。特点是小巧轻快,安全易用。
1.1 安装
命令:
yum install vsftpd -y配置vsftpd
安装完之后我们要对它进行配置,才能正常使用。
编辑vsftpd的配置文件
vi /etc/vsftpd/vsftpd.conf在配置文件中找到“anonymous_enable=yes”,将yes改为no,将匿名登录禁用。
添加开机自动启动,
chkconfig vsftpd on不确定是否已经加入了开机启动项可以运行chkconfig –list进行查看
1.2 创建用户
命令:useradd ftpuser
指定密码:passwd ftpuser
此用户就是用来登录ftp服务器用的。
这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为 /home/ftpuser.
———————————————————————————————————————————-
设置ftp用户的账号,例如账号为“ftpuser1”,目录为/home/ftpuser1,且设置不允许通过ssh登录。
useradd -d /home/ftpuser -s /sbin/nologin ftpuser 设置账号对应的密码,例如密码为“ftpuser”
passwd ftpuser1.3 开启selinux权限
vsftpd是一个ftp服务器程序,然后selinux是centos的防火墙组件。由于 vsftpd 默认被 selinux 拦截,所以会遇到的ftp以下的问题:
1.226 transfer done (but failed to open directory).(传输完成,但是打开路径失败)
2.550 failed to change directory(更改路径失败)
3.
553 could not create file.
4.
或者干脆在发送了list命令以后,服务器没响应,超时断开。
遇到这样的问题,通常是vsftpd 没有足够的权限,很有可能是被selinux阻止了。
查看命令:getsebool -a | grep ftp
getsebool -a | grep ftpd#以下是显示出来的权限,off是关闭权限,on是打开权限allow_ftpd_anon_write --> offallow_ftpd_full_access --> offallow_ftpd_use_cifs --> offallow_ftpd_use_nfs --> offftpd_connect_db --> offftpd_use_passive_mode --> offftp_home_dir --> off其中,ftp_home_dir和allow_ftpd_full_access必须为on 才能使vsftpd 具有访问ftp根目录,以及文件传输等权限。
设置命令:setsebool -p xxxxxx on
[root@bogon ~]# setsebool -p allow_ftpd_full_access on[root@bogon ~]# setsebool -p ftp_home_dir on1.4 设置或关闭防火墙
因为ftp默认的端口为21,而centos默认是没有开启的,所以要修改iptables文件
设置:vi /etc/sysconfig/iptables
在行上面有22 -j accept 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。
还要运行下,重启iptables
重启:service iptables restart
关闭防火墙:service iptables stop
禁用防火墙重启:chkconfig iptables off
1.5 启动vsftpd
命令:service vsftpd start
java客户端(代码)调用
package com.jonychen.util;import java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.ioexception;import java.io.inputstream;import java.net.socketexception;import java.util.date;import java.util.uuid;import org.apache.commons.net.ftp.ftp;import org.apache.commons.net.ftp.ftpclient;public class ftpuploadutil { private static threadlocal<ftpclient> threadlocal = new threadlocal<>(); private static threadlocal<string> threadlocalhost = new threadlocal<>(); public static void init(string host,int port,string username,string password) throws socketexception, ioexception { ftpclient client = threadlocal.get(); if(client==null) { client = new ftpclient(); //1.连接 client.connect(host,port); //2.登录 client.login(username,password); threadlocal.set(client); threadlocalhost.set(host); } } public static string upload(inputstream local,string filename,string path) throws socketexception, ioexception { string datepath = dateutil.date2str(new date(),/yyyy/mm/dd/); //路径添加日期 path =datepath; ftpclient client = threadlocal.get(); string host = threadlocalhost.get(); //3.指定文件上传路径(路径不存在返回false) boolean exists = client.changeworkingdirectory(path); if(!exists) { string patharray[] = path.split(/); string temp = /; for(string p:patharray) { temp =(p /); //4.如果文件路径不存在,则创建(一次只能创建一级目录) client.makedirectory(temp); } //重新指定文件上传路径 client.changeworkingdirectory(path); } //5.指定文件类型 client.setfiletype(ftp.binary_file_type); //获取后缀 string suffix = filename.substring(filename.lastindexof(.)); string uuid = uuid.randomuuid
代理网站在线升级-其他问题清除非法代码-虚拟主机/数据库问题淘宝/天猫会员合并为“88会员”美国云虚拟主机哪个好阿里云服务器费用加计扣除比例华为云云服务器的价格域名无访问-其他问题【黄山seo】完整的SEO需要包含哪些因素