iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot怎么使用FTP操作文件
  • 866
分享到

SpringBoot怎么使用FTP操作文件

2023-07-04 19:07:10 866人浏览 泡泡鱼
摘要

今天小编给大家分享一下SpringBoot怎么使用FTP操作文件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。简介使用 sp

今天小编给大家分享一下SpringBoot怎么使用FTP操作文件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

简介

使用 springBoot 配置 FTP 服务器,上传、删除、下载文件。

配置 FTP

检查是否安装 vsftpd

rpm -qa | grep vsftpd

检修是否已经安装 vsftpd 及查看版本号.

安装 vsftpd

yum -y install vsftpd

如果报错,则使用管理员权限执行 sudo yum -y install vsftpd

关闭匿名访问

关闭匿名访问后,想访问里面的文件就需要账号和密码;如果不关,就可以直接访问。

vim /etc/vsftpd/vsftpd.conf

如果提示是只读文件,那么你只需要输入命令: sudo vim /etc/vsftpd/vsftpd.conf

如下:

# 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

关闭匿名访问就是将:anonymous_enable=NO

启动服务

systemctl start vsftpd.service

查看服务状态

systemctl status vsftpd.service
[root@hadoop-master ~]# systemctl status vsftpd.service● vsftpd.service - Vsftpd ftp daemon   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)   Active: active (running) since 一 2022-12-19 10:15:39 CST; 58min ago  Process: 21702 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) Main PID: 21703 (vsftpd)   CGroup: /system.slice/vsftpd.service           └─21703 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf12月 19 10:15:39 hadoop-master systemd[1]: Starting Vsftpd ftp daemon...12月 19 10:15:39 hadoop-master systemd[1]: Started Vsftpd ftp daemon.[root@hadoop-master ~]#

看到绿色的 active(running),代表着启动成功正在运行中。

添加 FTP 用户

因为在 Linux 上,root 用户是不能登陆 FTP 的。如果你输入的是 root 用户,登陆会失败的。

adduser ftpadmin

设置密码:

passwd ftpadmin

输入两次密码就 ok 了。

配置允许root用户登录

/etc/vsftpd/user_list 文件和 /etc/vsftpd/ftpusers 文件中的root这一行注释掉

修改/etc/vsftpd/vsftpd.conf,在最后一行处添加local_root=/

service vsftpd  restart

这样远程就可以root用户身份登录ftp了。

文件存储地址授权

如存储地址为:app/upload/,设置权限为:

chmod 777 /app/upload/

SpringBoot 编码

添加依赖

<dependency>    <groupId>com.jcraft</groupId>    <artifactId>jsch</artifactId>    <version>0.1.55</version></dependency>

操作文件工具类

package com.demo.utils;import com.jcraft.jsch.*;import com.demo.dto.UploadFileDto;import lombok.extern.slf4j.Slf4j;import java.io.File;import java.io.FileOutputStream;import java.util.Properties;@Slf4jpublic class UploadFileUtils {        public static boolean upload(UploadFileDto dto) throws Exception {        log.info("============上传文件开始==============");        Boolean result = false;        ChannelSftp sftp = null;        Channel channel = null;        Session sshSession = null;        try {            JSch jSch = new JSch();            jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession = jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession.setPassword(dto.getPasswd());            Properties sshConfig = new Properties();            sshConfig.put("StrictHosTKEyChecking", "no");            sshSession.setConfig(sshConfig);            sshSession.connect();            channel = sshSession.openChannel("sftp");            channel.connect();            sftp = (ChannelSftp) channel;            sftp.cd(dto.getWorkingDir());            sftp.put(dto.getInputStream(), dto.getFileName());            result = true;            log.info("============上传文件结束==============");        } catch (JSchException e) {            result = false;            log.error("=====上传文件异常:{}", e.getMessage());            e.printStackTrace();        } finally {            closeChannel(sftp);            closeChannel(channel);            closeSession(sshSession);        }        return result;    }        public static boolean delete(UploadFileDto dto) throws Exception {        log.info("============删除文件开始==============");        Boolean result = false;        ChannelSftp sftp = null;        Channel channel = null;        Session sshSession = null;        try {            JSch jSch = new JSch();            jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession = jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession.setPassword(dto.getPasswd());            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            sshSession.setConfig(sshConfig);            sshSession.connect();            channel = sshSession.openChannel("sftp");            channel.connect();            sftp = (ChannelSftp) channel;            sftp.cd(dto.getWorkingDir());            sftp.rm(dto.getFileName());            result = true;            log.info("============删除文件结束==============");        } catch (JSchException e) {            result = false;            log.error("=====删除文件异常:{}", e.getMessage());            e.printStackTrace();        } finally {            closeChannel(sftp);            closeChannel(channel);            closeSession(sshSession);        }        return result;    }        public static boolean download(UploadFileDto dto) throws Exception {        log.info("============下载文件开始==============");        Boolean result = false;        ChannelSftp sftp = null;        Channel channel = null;        Session sshSession = null;        try {            JSch jSch = new JSch();            jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession = jSch.getSession(dto.getAccount(), dto.getHost(), dto.getPort());            sshSession.setPassword(dto.getPasswd());            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            sshSession.setConfig(sshConfig);            sshSession.connect();            channel = sshSession.openChannel("sftp");            channel.connect();            sftp = (ChannelSftp) channel;            sftp.cd(dto.getWorkingDir());            sftp.get(dto.getFileName(), new FileOutputStream(new File(dto.getDownloadPath())));            sftp.disconnect();            sftp.getSession().disconnect();            result = true;            log.info("============下载文件结束==============");        } catch (JSchException e) {            result = false;            log.error("=====下载文件异常:{}", e.getMessage());            e.printStackTrace();        } finally {            closeChannel(sftp);            closeChannel(channel);            closeSession(sshSession);        }        return result;    }    private static void closeChannel(Channel channel) {        if (channel != null) {            if (channel.isConnected()) {                channel.disconnect();            }        }    }    private static void closeSession(Session session) {        if (session != null) {            if (session.isConnected()) {                session.disconnect();            }        }    }}

UploadFileDto.java

package com.demo.dto;import io.swagger.annotations.apiModel;import io.swagger.annotations.ApiModelProperty;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import java.io.InputStream;@Data@AllArgsConstructor@NoArgsConstructor@Builder@ApiModel(value = "上传文件Dto")public class UploadFileDto {    @ApiModelProperty(value = " ftp 服务器ip地址")    private String host;    @ApiModelProperty(value = " ftp 服务器port,默认是21")    private Integer port;    @ApiModelProperty(value = " ftp 服务器用户名")    private String account;    @ApiModelProperty(value = " ftp 服务器密码")    private String passwd;    @ApiModelProperty(value = " ftp 服务器存储图片的绝对路径")    private String workingDir;    @ApiModelProperty(value = "上传到ftp 服务器文件名")    private String fileName;    @ApiModelProperty(value = " 文件流")    private InputStream inputStream;    @ApiModelProperty(value = " 下载文件的路径")    private String downloadPath;}

UploadVo.java

package com.demo.vo;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructor@Builder@ApiModel(value = "文件VO")public class UploadVo {    @ApiModelProperty(value = "原始文件名称")    private String oldName;    @ApiModelProperty(value = "新文件名称")    private String newName;    @ApiModelProperty(value = "访问路径")    private String path;}

UploadController

package com.demo.controller;import com.demo.vo.UploadVo;import com.demo.service.UploadService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.WEB.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;@Slf4j@RestController@RequestMapping("/upload")@Api(value = "upload", tags = "上传文件")public class UploadController {    @Autowired    private UploadService uploadService;    @ApiOperation(value = "上传图片", notes = "上传图片")    @PostMapping("/uploadImage")    public UploadVo uploadImage(@RequestParam("file") MultipartFile file) {        return uploadService.uploadImage(file);    }    @ApiOperation(value = "删除文件", notes = "删除文件")    @GetMapping("/delFile")    public Boolean delFile(String fileName) {        return uploadService.delFile(fileName);    }    @ApiOperation(value = "下载文件", notes = "下载文件")    @GetMapping("/downloadFile")    public Boolean downloadFile(String fileName, String downloadPath) {        return uploadService.downloadFile(fileName, downloadPath);    }}

UploadService

package com.demo.service;import com.demo.vo.UploadVo;import org.springframework.web.multipart.MultipartFile;public interface UploadService {    UploadVo uploadImage(MultipartFile file);    Boolean delFile(String fileName);    Boolean downloadFile(String fileName, String downloadPath);}

UploadServiceImpl

package com.demo.service.impl;import com.demo.dto.UploadFileDto;import com.demo.vo.UploadVo;import com.demo.config.FtpConfig;import com.demo.service.UploadService;import com.demo.utils.UUIDUtils;import com.demo.utils.UploadFileUtils;import com.demo.exception.BusinessException;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.Objects;@Slf4j@Servicepublic class UploadServiceImpl implements UploadService {    @Autowired    private FtpConfig ftpConfig;    @Override    public UploadVo uploadImage(MultipartFile file) {        log.info("=======上传图片开始,图片名称:{}", file.getOriginalFilename());        try {            // 1. 取原始文件名            String oldName = file.getOriginalFilename();            // 2. ftp 服务器的文件名            String newName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + UUIDUtils.getUUID(10) + oldName.substring(oldName.lastIndexOf("."));            // 3.上传图片            Boolean result = UploadFileUtils.upload(                    UploadFileDto.builder()                            .host(ftpConfig.host)                            .port(ftpConfig.post)                            .account(ftpConfig.username)                            .passwd(ftpConfig.passWord)                            .workingDir(ftpConfig.basePath)                            .fileName(newName)                            .inputStream(file.getInputStream())                            .build()            );            // 4.返回结果            if (!result) {                throw new BusinessException("上传图片失败!");            }            log.info("=======上传图片结束,新图片名称:{}", newName);            return UploadVo.builder()                    .oldName(oldName)                    .newName(newName)                    .path(ftpConfig.imageBaseUrl + "/" + newName)                    .build();        } catch (Exception e) {            log.error("=======上传图片异常,异常信息:{}", e.getMessage());            e.printStackTrace();        }        return null;    }    @Override    public Boolean delFile(String fileName) {        if (Objects.isNull(fileName)) {            throw new BusinessException("文件名称为空,请核实!");        }        try {            Boolean result = UploadFileUtils.delete(                    UploadFileDto.builder()                            .host(ftpConfig.host)                            .port(ftpConfig.post)                            .account(ftpConfig.username)                            .passwd(ftpConfig.password)                            .workingDir(ftpConfig.basePath)                            .fileName(fileName)                            .build()            );            return result;        } catch (Exception e) {            log.error("=======删除文件异常,异常信息:{}", e.getMessage());            e.printStackTrace();        }        return null;    }    @Override    public Boolean downloadFile(String fileName, String downloadPath) {        if (Objects.isNull(fileName) || Objects.isNull(downloadPath)) {            throw new BusinessException("文件名称或下载路径为空,请核实!");        }        try {            Boolean result = UploadFileUtils.download(                    UploadFileDto.builder()                            .host(ftpConfig.host)                            .port(ftpConfig.post)                            .account(ftpConfig.username)                            .passwd(ftpConfig.password)                            .workingDir(ftpConfig.basePath)                            .fileName(fileName)                            .downloadPath(downloadPath)                            .build()            );            return result;        } catch (Exception e) {            log.error("=======下载文件异常,异常信息:{}", e.getMessage());            e.printStackTrace();        }        return null;    }}

FtpConfig

package com.demo.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class FtpConfig {    // ftp 服务器ip地址    @Value("${ftp.host}")    public String host;    // ftp 服务器port,默认是21    @Value("${ftp.post}")    public Integer post;    // ftp 服务器用户名    @Value("${ftp.username}")    public String username;    // ftp 服务器密码    @Value("${ftp.password}")    public String password;    // ftp 服务器存储图片的绝对路径    @Value("${ftp.base-path}")    public String basePath;    // ftp 服务器外网访问图片路径    @Value("${ftp.image-base-url}")    public String imageBaseUrl;}

application.yml

# ftpftp:  host: 127.0.0.1  post: 22  username: ftpadmin  password: ftpadmin  base-path: /app/upload/images  image-base-url: Http://127.0.0.1:8080/images

配置 Nginx

server {    listen       8080;    server_name  localhost;    location /images/ {        root  /app/upload/;        autoindex on;    }}

以上就是“SpringBoot怎么使用FTP操作文件”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。

--结束END--

本文标题: SpringBoot怎么使用FTP操作文件

本文链接: https://www.lsjlt.com/news/347281.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot怎么使用FTP操作文件
    今天小编给大家分享一下SpringBoot怎么使用FTP操作文件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。简介使用 Sp...
    99+
    2023-07-04
  • SpringBoot使用FTP操作文件的过程(删除、上传、下载文件)
    目录简介配置 FTPSpringBoot 编码配置 Nginx简介 使用 SpringBoot 配置 FTP 服务器,上传、删除、下载文件。 配置 FTP 检查是否安装 vsftpd...
    99+
    2022-12-20
    SpringBoot FTP 操作文件 SpringBoot FTP 操作文件
  • 使用Springboot整合GridFS实现文件操作
    目录GridFsOperations,实现GridFS文件上传下载删除上传下载删除功能实现测试上传下载删除GridFsOperations,实现GridFS文件上传下载删除 最近学习...
    99+
    2024-04-02
  • 怎么使用Matlab操作HDF5文件
    这篇文章主要介绍“怎么使用Matlab操作HDF5文件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用Matlab操作HDF5文件”文章能帮助大家解决问题。HDF5文件在使用Matlab对数据...
    99+
    2023-07-02
  • 怎么使用Python操作HDF5文件
    这篇文章主要介绍“怎么使用Python操作HDF5文件”,在日常操作中,相信很多人在怎么使用Python操作HDF5文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用Python操作HDF5文件”的疑...
    99+
    2023-07-02
  • Python怎么使用FTP上传文件
    这篇“Python怎么使用FTP上传文件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python怎么使用FTP上传文件”文...
    99+
    2023-07-05
  • 怎么在python中使用shutil操作文件
    怎么在python中使用shutil操作文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。python有哪些常用库python常用的库:1.requesuts;2.scrapy...
    99+
    2023-06-14
  • 使用Python怎么对文件进行操作
    使用Python怎么对文件进行操作?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。文件的存储方式在计算机中,文件是以 二进制的方式保存在磁盘上的文本文件和二进制文...
    99+
    2023-06-15
  • 使用Python操作PDF文件
    从PDF读取文本内容和从已经有的文档生成新的PDF。 需要用到的模块是PyPDF2. mstamy2/PyPDF2: A utility to read and write PDFs...
    99+
    2024-04-02
  • Linux中怎么使用tar命令操作文件
    这篇文章将为大家详细讲解有关Linux中怎么使用tar命令操作文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。语法     tar [-] A --...
    99+
    2023-06-12
  • 怎么使用Python超过99%的文件操作
    本篇内容主要讲解“怎么使用Python超过99%的文件操作”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Python超过99%的文件操作”吧!一、打开和关闭文件当您要读取或写入文件时,首...
    99+
    2023-06-16
  • 怎么在java中使用FTP下载文件
    怎么在java中使用FTP下载文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向对象编程语言的代表,实现了面...
    99+
    2023-06-14
  • springboot对本地文件进行操作
    文章目录 方案一:使用ResourceUtils方案二:使用commons-io方案三:springboot获得本地磁盘文件路径方案四:通过ResourceLoader使用文件流的方式来读取J...
    99+
    2023-09-23
    spring boot java
  • Linux中怎么使用get获取ftp文件
    本篇内容介绍了“Linux中怎么使用get获取ftp文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!get  使用lftp登录f...
    99+
    2023-06-28
  • SpringBoot读取yaml文件操作详解
    目录1. 单个属性2. 全部属性3. 对象属性补充1. 单个属性 yaml 内的属性如下: server: port: 80 只需在成员变量上注解 @Value(“...
    99+
    2024-04-02
  • python怎么操作文本文件
    使用python操作文本文件的方法:1.新建python项目;2.使用open()函数打开txt文本文件;3.使用write()方法向文件追加内容;4.使用close()函数关闭文件;具体步骤如下:首先,打开python,并新建一个pyth...
    99+
    2024-04-02
  • SpringBoot中怎么使用JdbcTemplate操作数据库
    本篇内容介绍了“SpringBoot中怎么使用JdbcTemplate操作数据库”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、创建表CR...
    99+
    2023-07-06
  • 如何使用JS操作文件
    这篇文章主要讲解了“如何使用JS操作文件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用JS操作文件”吧!JS读取文件 FileReaderFileReader 对象允许Web应用程序...
    99+
    2023-06-22
  • SpringBoot 使用Mongo的GridFs实现分布式文件存储操作
    目录前言GridFs介绍什么时候使用GridFsGridFs的原理环境引入依赖和项目配置使用GridFsTemplate操作GridFs前言 这段时间在公司实习,安排给我一个任务,让...
    99+
    2024-04-02
  • Python使用FTP上传文件
    Python使用FTP上传文件 本文主要介绍如何使用Python通过FTP上传文件。 FTP简介 FTP即文件传输协议(File Transfer Protocol),是用于在网络上进行文件传输的一种...
    99+
    2023-09-07
    python 服务器 网络
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作