Centos7搭建ftp服务器
2017-11-28 17:47
447 查看
Centos7搭建ftp服务器
支持访问方式:
1.浏览器访问方式:打开浏览器,在地址栏中输入ftp://服务器IP地址。然后回车。
2.磁盘访问方式:双击计算机或者我的电脑,打开磁盘对话框,然后在路径栏中输入
ftp://服务器IP地址。然后回车。
3. 客户端访问方式。
准备安装
准备工作:- 系统镜像——CentOS-7-x86_64-DVD-1511.iso
- 配置ip地址——如我的ip
192.168.133.44,配置ip参考
- 查看是否已经安装vsftpd,如果没有就安装,查看命令:
rpm -qa | grep vsftpd
一、安装vsftpd组件
安装完后,有/etc/vsftpd/vsftpd.conf文件,是vsftp的配置文件,安装命令如下:
yum -y install vsftpd
二、添加一个ftp用户
此用户就是用来登录ftp服务器用的。命令如下:useradd ftpuser
这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为
/home/ftpuser
三、 给ftp用户添加密码
执行以下命令,设置密码。命令如下:passwd ftpuser
四、关闭防火墙
ftp默认的端口为21,而centos默认是没有开启的。所以需要修改防火墙文件,添加开启21端口配置。本文以直接关闭防火墙为例。命令如下:systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 firewall-cmd --state #查看默认防火墙状态
五、修改selinux开启外网的访问
1)查看状态。命令如下:getsebool -a | grep ftp
2)状态显示:
getsebool -a | grep ftp allow_ftpd_anon_write --> off allow_ftpd_full_access --> off allow_ftpd_use_cifs --> off allow_ftpd_use_nfs --> off ftp_home_dir --> off ftpd_connect_db --> off ftpd_use_passive_mode --> off httpd_enable_ftp_server --> off tftp_anon_write --> off
3)执行以下两条命令,开启外网的访问。命令如下:
setsebool -P allow_ftpd_full_access on
setsebool -P ftp_home_dir on
六、关闭匿名访问
1)修改/etc/vsftpd/vsftpd.conf文件。命令如下:
vim /etc/vsftpd/vsftpd.conf
2)重启ftp服务。命令如下:
service vsftpd restart
七、开启被动模式
默认是开启的,但是要指定一个端口范围,打开/etc/vsftpd/vsftpd.conf文件,在后面加上
pasv_min_port=30000 pasv_max_port=30999
表示端口范围为30000~30999,这个可以随意改。改完重启一下vsftpd
service vsftpd restart
由于指定的这段端口范围默认是没有开启的,所以防火墙若
采用开放端口方式:相应的开启这个范围端口
直接关闭方式:不理会
八、设置开机启动vsftpd ftp服务
chkconfig vsftpd on九、访问ftp
打开Windows 资源管理器,上方输入
ftp://192.168.133.44/,回车。
输入步骤二设置好用户名和密码进入。默认进入目录
/home/ftpuser
若想操作其它目录,需注意该目录权限问题,可通过
chmod -R 777 /xxx/xxx授权
十、 java访问ftp
涉及的jar包:commons-net-3.6.jar
代码清单如下:
package com.zlop; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * ftp上传下载工具类 * <p>Title: FtpUtil</p> * <p>Description: </p> * @author 曾令鹏 * @date 2017年11月26日下午18:11:51 * @version 1.0 */ public class FTPUtil { /** * Description: 向FTP服务器上传文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param basePath FTP服务器基础目录 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2017/11/26。文件的路径为basePath+filePath * @param filename 上传到FTP服务器上的文件名 * @param input 输入流 * @return 成功返回true,否则返回false */ public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port);// 连接FTP服务器 // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //切换到上传目录 if (!ftp.changeWorkingDirectory(basePath+filePath)) { //如果目录不存在创建目录 String[] dirs = filePath.split("/"); String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } //设置上传文件的类型为二进制类型 ftp.setFileType(FTP.BINARY_FILE_TYPE); //上传文件 if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 从FTP服务器下载文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登录账号 * @param password FTP登录密码 * @param remotePath FTP服务器上的相对路径 * @param fileName 要下载的文件名 * @param localPath 下载后保存到本地的路径 * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static void main(String[] args) { try { FileInputStream in=new FileInputStream(new File("C:\\Users\\zenglingpeng\\Desktop\\fdfs\\source\\cc.jpg")); boolean flag = uploadFile("192.168.133.44", 21, "ftpuser", "123456", "/home/ftpuser","/", "cc.jpg", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
综上,基本就走完了。
相关文章推荐
- Linux[Centos] FTP服务器搭建步骤
- CentOS6.5搭建ftp服务器
- CentOS7种搭建FTP服务器
- CentOS6.5下搭建FTP服务器 + Java上传文件
- Centos 6.5 FTP 文件服务器搭建
- 在CentOS中搭建ftp服务器出现的问题
- Centos 下搭建FTP上传下载服务器
- centos环境搭建java web,mysql,ftp服务器环境
- CentOS 下搭建FTP服务器
- centos6.4 上ftp服务器搭建
- Centos 搭建ftp服务器
- CentOS 6.5下快速搭建ftp服务器
- linux(centos6)搭建ftp服务器 -摘自网络
- (转)Centos搭建FTP服务器
- centos6.5搭建FTP服务器
- CentOS 6.5下快速搭建ftp服务器
- centos搭建ftp服务器
- centOS7下搭建FTP服务器详细步骤
- Centos 6.4搭建FTP服务器
- CentOS 5.8下快速搭建FTP服务器