ftp在linux部署及其安装

安装ftp服务

yum install -y vsftpd

查看一下是否安装成功

rpm -qa |grep ‘vsftpd’

ftp的配置文件主要有三个,位于/etc/vsftpd/目录下,分别是:

ftpusers:用来指定哪些用户不能访问ftp服务器

user_list::这个文件中的用户是否可以登录到服务器,取决于vsftpd.conf文件中的userlist_enable和userlist_deny这两个选项。

vsftpd.conf:ftp服务器的主配置文件

添加ftp用户(上传的时候有用)

useradd -d /data/ftp -s /sbin/nologin ftpuser

用户配置密码,需要在root用户下

passwd ftpuser
xxx

启动服务

#启动ftp命令

service vsftpd start


#停止ftp命令

service vsftpd stop


#重启ftp命

service vsftpd restart


#重载

service vsftpd reload

修改ftp配置  注释可以忽略,看看最下面增加的配置就行

# Example 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.
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=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# 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=NO
#
# 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=YESpam_service_name=vsftpd
userlist_enable=YESpam_service_name=vsftpd
userlist_enable=YES
userlist_deny=no
userlist_file=/etc/vsftpd/user_list  

别忘记在用户列表文件  user_list 加上自己的用户名 ftpuser

# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
#root
#bin
#daemon
#adm
#lp
#sync
#shutdown
#halt
#mail
#news
#uucp
#operator
#games
#nobody
ftpuser

下面是上传ftp的java程序

    //利用ftp技术,将文件上传到ftp服务器@CrossOrigin(origins = "*") // 允许任何来源的请求@PostMapping("/uploadFileToFTP")//ftp 上传方法public boolean uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("batchId") String batchId) throws IOException {boolean flag = uploadFile(FTPProperties.getServer(), FTPProperties.getPort(), FTPProperties.getUsername(),FTPProperties.getPassword(), batchId, "video", file.getOriginalFilename(), file.getInputStream());//根据反馈输出日志if (flag) {log.info("上传成功,文件名为:" + file.getOriginalFilename() + "文件大小=" + file.getSize() + "字节" + "MB=" + file.getSize() / 1024 / 1024 + "MB");} else {log.error("上传失败,文件名为:" + file.getOriginalFilename() + "文件大小=" + file.getSize() + "字节" + "MB=" + file.getSize() / 1024 / 1024 + "MB");}return flag;}/*** #!/bin/bash* <p>* # 安装vsftpd* sudo yum remove vsftpd -y* sudo yum install vsftpd -y* <p>* # 启动vsftpd服务* sudo systemctl start vsftpd* <p>* # 检查服务状态* sudo systemctl status vsftpd* 云服务器记得开端口* <p>* 创建一个新用户* 切记 必须在/etc/vsftpd/user_list* <p>* 指定允许使用vsftpd的用户列表文件=》 也就是自己的用户名,要不然访问530错误** @param url* @param port* @param username* @param password* @param path* @param path1* @param filename* @param input* @return*/public boolean uploadFile(String url,// FTP服务器hostnameint port,// FTP服务器端口String username, // FTP登录账号String password, // FTP登录密码String path, // FTP服务器保存目录String path1, // FTP服务器保存目录1String filename, // 上传到FTP服务器上的文件名InputStream input // 输入流) {boolean success = false;FTPClient ftp = new FTPClient();ftp.setControlEncoding("UTF-8");try {boolean b = false;int reply;ftp.connect(url, port);// 连接FTP服务器// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器ftp.login(username, password);// 登录reply = ftp.getReplyCode();// 获取服务器的响应码。if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();return success;}ftp.setFileType(FTPClient.BINARY_FILE_TYPE);ftp.makeDirectory(path);  //创建文件夹ftp.changeWorkingDirectory(path);  //切换到文件夹
//            ftp.makeDirectory(path1);
//            ftp.changeWorkingDirectory(path1);b = ftp.storeFile(filename, input);//最终默认上传到 /home/ftpuser 目录下  批次id目录内if (b) {//异步写入日志asynWriteVideoImportLog(path, filename, input);}input.close();ftp.logout();success = b;} catch (IOException e) {e.printStackTrace();} finally {if (ftp.isConnected()) {try {ftp.disconnect();} catch (IOException ioe) {}}}return success;}@Async("normalThreadPool")public void asynWriteVideoImportLog(String path, String filename, InputStream input) throws IOException {//写入日志FtpImportTableEntity ftpImportTableEntity = new FtpImportTableEntity();ftpImportTableEntity.setFileName(filename);ftpImportTableEntity.setAddress(path + "/" + filename);ftpImportTableEntity.setSize((int) input.available());ftpImportTableEntity.setTester(filename.split("_")[1]);ftpImportTableEntity.setTime(LocalDateTime.now());ftpImportTableEntity.setBatch(Long.parseLong(path));ftpImportTableService.save(ftpImportTableEntity);}@CrossOrigin(origins = "*") // 允许任何来源的请求@GetMapping("/ftp/getVideo")public void getVideo(HttpServletResponse response, @RequestParam String videoPath, @RequestParam String videoName) {FTPClient ftp = new FTPClient();ftp.setControlEncoding("UTF-8");try {ftp.connect(FTPProperties.getServer(), FTPProperties.getPort());ftp.login(FTPProperties.getUsername(), FTPProperties.getPassword());int reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();throw new RuntimeException("FTP server refused connection.");}ftp.setFileType(FTPClient.BINARY_FILE_TYPE);InputStream inputStream = ftp.retrieveFileStream(videoPath + "/" + videoName);if (inputStream != null) {try (OutputStream outputStream = response.getOutputStream()) {byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}outputStream.flush(); // 确保所有数据已写入输出流}// 关闭输出流response.flushBuffer();inputStream.close();ftp.logout(); // 登出} else {throw new RuntimeException("文件未找到");}} catch (IOException e) {
//            throw new RuntimeException("Error downloading file from FTP server", e);} finally {if (ftp.isConnected()) {try {ftp.disconnect();} catch (IOException e) {// log error or handle as needed}}}}@CrossOrigin(origins = "*") // 允许任何来源的请求@GetMapping("/ftp/getVideoList")public List<VideoInfo> getVideoList() {List<VideoInfo> videoList = new ArrayList<>();// 连接FTP服务器FTPClient ftp = new FTPClient();ftp.setControlEncoding("UTF-8");try {int reply;ftp.connect(FTPProperties.getServer(), FTPProperties.getPort());ftp.login(FTPProperties.getUsername(), FTPProperties.getPassword());reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();}FTPFile[] files = ftp.listFiles(); // 获取FTP服务器上所有文件for (FTPFile file : files) {listFilesRecursive(ftp, "/home/ftpuser", videoList);}} catch (Exception e) {e.printStackTrace();} finally {try {ftp.logout();ftp.disconnect();} catch (IOException e) {e.printStackTrace();}}return videoList;}private void listFilesRecursive(FTPClient ftp, String path, List<VideoInfo> videoList) throws IOException {FTPFile[] files = ftp.listFiles(path);for (FTPFile file : files) {if (file.isDirectory()) {// 如果是文件夹,递归进入文件夹listFilesRecursive(ftp, path + "/" + file.getName(), videoList);} else if (file.getName().toLowerCase().endsWith(".mp4")) {// 处理视频文件String videoPath = path; // 视频文件路径String videoName = file.getName(); // 视频文件名称videoList.add(new VideoInfo(videoPath, videoName));}}}

上传文件是默认存在/home/ftpuser里的,程序里你定义什么文件夹,都会在/home/ftpuser内进行,下面如果你想指定ftp上传那个文件夹怎么办?

下面是如何修改配置,让其可以传到指定文件夹:

命令行三行即可,假如我想把上传的视频配置到nginx的文件目录下,

#修改指定用户的指定文件目录
usermod -d /home/nginx/www/images ftpuser
#
#给权限
chown -R  ftpuser:ftpuser   /home/nginx/www/images 
#重启
systemctl restart vsftpd.service

想知道我怎么让其使用nginx部署视频播放服务器,请看我的另一篇文章

《nginx部署视频服务(视频下载链接也ok),vue前端如何播放视频》

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/787501.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java的Math类,System类,Runtime类和BigDemical类常用的API

Math类&#xff1a; 代表数学&#xff0c;是一个工具类&#xff1b; public static int abs(int a) Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument i…

​慧天[HTWATER]可以与与SWMM模型之间实现转换吗?

​慧天[HTWATER]软件简介 针对城市排水系统基础设施数据管理的需求&#xff0c;以及水文、水力及水质模拟对数据的需求&#xff0c;实现了以数据库方式对相应数据的存储。可以对分流制排水系统及合流制排水系统进行地表水文、管网水力、水质过程的模拟计算。可以对城市低影响开…

SwiftUI Swift 显示隐藏系统顶部状态栏

Show me the code // // TestHideSystemTopBar.swift // pandabill // // Created by 朱洪苇 on 2024/4/1. //import SwiftUIstruct TestHideSystemTopBar: View {State private var isStatusBarHidden falsevar body: some View {Button {withAnimation {self.isStatusBa…

有单片机基础,准备过一遍freertos,之后再学linux

应届生我觉得继续把单片机深入一下比较好。 站在公司的角度思考一下&#xff0c;个人觉得应该有以下结论&#xff1a; 1 大公司具备技术资金等优势&#xff0c;因此喜欢学历好&#xff0c;理论扎实能够攻坚的人。倾向于培养各个领域的专家&#xff0c;共同构建自己产品的竞争…

strormpot对象池

设计思想 Stormpot 是一个用 Java 编写的高性能对象池库&#xff0c;设计思想侧重于提供高吞吐量和低延迟的对象分配&#xff0c;同时保证线程安全和轻量级。其核心思想是通过一个高效的对象池来管理对象的生命周期&#xff0c;确保对象的重用&#xff0c;并减少垃圾收集器的压…

金三银四求职季,最新面试题分享和解析,建议收藏

随着金三银四招聘旺季的到来&#xff0c;在这个竞争激烈的时刻&#xff0c;了解最新的软件测试面试题&#xff0c;充分准备面试&#xff0c;对求职者来说显得尤为重要。本文将分享一些金三银四期间最新的软件测试面试题&#xff0c;帮助求职者更好地应对面试挑战。 一.登录页面…

企业如何通过AARRR模型完成快速扩张?

在竞争激烈的市场环境中&#xff0c;企业要想实现持续的用户增长&#xff0c;就需要采用一套有效的策略和模型。AARRR模型→&#xff08;【1日1词】AARRR用户增长模型--互联网运营经典模型&#xff09;是一个帮助企业实现用户增长的有效方法。下面将详细阐述企业在营销运营过程…

OpenHarmony实战开发-如何通过Stage模型实现一个简单的游戏卡片

介绍 本示例展示了如何通过Stage模型实现一个简单的游戏卡片。 通过卡片支持的点击事件进行交互&#xff0c;让用户通过点击的先后顺序把一个乱序的成语排列成正确的成语。使用了C和TS的混合编程方式&#xff0c;将获取随机数的能力下沉到C实现&#xff0c;并通过NAPI的能力将…

Adaboost集成学习 | Matlab实现基于GRU-Adaboost门控循环单元结合Adaboost集成学习时间序列预测(股票价格预测)

目录 效果一览基本介绍模型设计程序设计参考资料效果一览 基本介绍 Adaboost集成学习 | Matlab实现基于GRU-Adaboost门控循环单元结合Adaboost集成学习时间序列预测(股票价格预测) 模型设计 股票价格预测是一个具有挑战性的时间序列预测问题,可以使用深度学习模型如门控循环…

常用运动模型

运动模型 常用运动模型: CV、CA、CTRV、CTRV、CTRA、CSAV和CCA/CSAA模型微分多项式模型辛格模型半马尔科夫模型机动目标"当前模型"二维转弯运动模型三维模型比列导引模型 恒定速度模型&#xff08;Constant Velocity, CV&#xff09; 恒定加速度模型&#xff08;C…

websocket 局域网 webrtc 一对一 多对多 视频通话 的示例

基本介绍 WebRTC&#xff08;Web Real-Time Communications&#xff09;是一项实时通讯技术&#xff0c;它允许网络应用或者站点&#xff0c;在不借助中间媒介的情况下&#xff0c;建立浏览器之间点对点&#xff08;Peer-to-Peer&#xff09;的连接&#xff0c;实现视频流和&am…

CSS3 高级- 复杂选择器、内容生成、变形(transform)、过渡(transition)、动画(animation)

文章目录 一、复杂选择器兄弟选择器:选择平级元素的唯一办法属性选择器:1、通用:基本用不着,太泛了2、自定义:4种伪类选择器:1、目标伪类:2、结构伪类:3、元素状态伪类:4、伪元素选择器:应用于文字,使网页看起来想杂志5、否定伪类:选择器:not([本选择器的条件]) /*…

【算法刷题day14】Leetcode:144.二叉树的前序遍历、94.二叉树的中序遍历、145.二叉树的后序遍历

文章目录 二叉树递归遍历解题思路代码总结 二叉树的迭代遍历解题思路代码总结 二叉树的统一迭代法解题思路代码总结 草稿图网站 java的Deque 二叉树递归遍历 题目&#xff1a; 144.二叉树的前序遍历 94.二叉树的中序遍历 145.二叉树的后序遍历 解析&#xff1a;代码随想录解析…

Vue3学习和进阶

Vue的简介 一、什么是vue? Vue是一款用于构建用户界面的javascript框架。它基于标准的HTML、CSS、JS进行构建&#xff0c;并且提供的一套声明式的&#xff0c;组件化的编程模型&#xff0c;进而帮助我们高效开发用户界面。无论是复杂还是简单的界面&#xff0c;我们都可以用…

mongodb的简单操作

文章目录 前言数据库的创建和删除集合的创建和删除文档的插入和查询异常处理更新数据局部修改符合条件的批量更新加操作 删除文档删除全部数据删除符合条件的数据 统计count统计有多少条数据统计特定条件有多少条数据 分页查询排序查询正则查询比较查询包含查询条件连接查询索引…

【文件操作API的使用】

1.概念 这对聪明的你们来说简直就是&#xff0c;对吗。 那什么是文件操作符&#xff0c;文件操作又有哪些步骤呢&#xff1f; 文件操作符通常用于指代在计算机编程中用于处理文件的特殊符号或标识符。在很多编程语言中&#xff0c;文件操作符被用于打开、关闭、读取和写入文件…

香港科技大学广州|数据科学与分析学域硕博招生宣讲会—天津大学专场

时间&#xff1a;2024年4月12日&#xff08;星期五&#xff09;14:00 地点&#xff1a;天津大学北洋园校区55楼B204 报名链接&#xff1a;https://www.wjx.top/vm/Q0cKTUI.aspx# 跨学科研究领域 *数据驱动的人工智能和机器学习 *统计学习和建模 工业和商业分析 *特定行业的数…

两阶段提交-mysql

二阶段提交的 prepare 阶段&#xff0c;InnoDB 主要做五件事。 第 1 件&#xff0c;把分配给事务的所有 undo 段的状态从 TRX_UNDO_ACTIVE 修改为 TRX_UNDO_PREPARED。 进入二阶段提交的事务&#xff0c;都至少改变过&#xff08;插入、更新、删除&#xff09;一个用户表的一…

工艺品wordpress外贸主题

工艺品wordpress外贸主题 简约大气的wordpress外贸主题&#xff0c;适合做工艺品进出品外贸的公司官网使用。 https://www.jianzhanpress.com/?p5377

社交媒体市场:揭示Facebook的商业模式

在数字化时代&#xff0c;社交媒体已经成为人们生活中不可或缺的一部分。Facebook作为全球最大的社交媒体平台之一&#xff0c;其商业模式的运作方式对于了解社交媒体市场的发展趋势和影响力至关重要。本文将深入探讨Facebook的商业模式&#xff0c;剖析其运作机制&#xff0c;…