search
HomeJavajavaTutorialHow SpringBoot uses FTP to operate files

Introduction

Use SpringBoot to configure the FTP server to upload, delete, and download files.

Configuring FTP

Check whether vsftpd is installed

rpm -qa | grep vsftpd

Check whether vsftpd is installed and check the version number.

Install vsftpd

yum -y install vsftpd

If an error is reported, use administrator rights to execute sudo yum -y install vsftpd

Turn off anonymous access

After turning off anonymous access, you need an account and password to access the files inside; if you don't turn it off, you can access it directly.

vim /etc/vsftpd/vsftpd.conf

If the prompt is a read-only file, then you only need to enter the command: sudo vim /etc/vsftpd/vsftpd.conf

is as follows :

# 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

To turn off anonymous access is to: anonymous_enable=NO

Start the service

systemctl start vsftpd.service

View the service status

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.conf

12月 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 ~]#

See the green active (running), which means the startup is successful and running.

Add FTP user

Because on Linux, the root user cannot log in to FTP. If you enter the root user, the login will fail.

adduser ftpadmin

Set password:

passwd ftpadmin

Enter the password twice and it will be ok.

Configure to allow root user to log in

Place the /etc/vsftpd/user_list file and /etc/vsftpd/ftpusers Comment out the root line in the file

Modify /etc/vsftpd/vsftpd.conf and add local_root=/ to the last line

service vsftpd  restart

In this way, you can remotely log in to ftp as the root user.

File storage address authorization

If the storage address is: app/upload/, set the permission to:

chmod 777 /app/upload/

SpringBoot Coding

Add dependency

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

Operation file tool class

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;

/**
 * @ClassName: UploadFileUtils.java
 * @Description: 上传文件
 * @Author: tanyp
 * @Date: 2022/12/19 10:38
 **/
@Slf4j
public class UploadFileUtils {

    /**
     * @MonthName: upload
     * @Description: 上传文件
     * @Author: tanyp
     * @Date: 2022/12/19 10:38
     * @Param: [dto]
     * @return: boolean
     **/
    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;
    }

    /**
     * @MonthName: delete
     * @Description: 删除文件
     * @Author: tanyp
     * @Date: 2022/12/19 10:38
     * @Param: [dto]
     * @return: boolean
     **/
    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;
    }

    /**
     * @MonthName: download
     * @Description: 下载文件
     * @Author: tanyp
     * @Date: 2022/12/19 10:38
     * @Param: [dto]
     * @return: boolean
     **/
    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;

/**
 * @ClassName: UploadFileDto.java
 * @ClassPath: com.demo.dto.UploadFileDto.java
 * @Description: 上传文件
 * @Author: tanyp
 * @Date: 2022/12/19 10:38
 **/
@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;

/**
 * @ClassName: UploadVo.java
 * @ClassPath: com.demo.vo.UploadVo.java
 * @Description: 文件VO
 * @Author: tanyp
 * @Date: 2022/12/19 15:18
 **/
@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;

/**
 * @ClassName: UploadController.java
 * @ClassPath: com.demo.controller.UploadController.java
 * @Description: 上传文件
 * @Author: tanyp
 * @Date: 2022/12/19 15:18
 **/
@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;

/**
 * @ClassName: UploadService.java
 * @ClassPath: com.demo.service.UploadService.java
 * @Description:上传文件
 * @Author: tanyp
 * @Date: 2022/12/19 15:18
 **/
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;

/**
 * @ClassName: UploadServiceImpl.java
 * @ClassPath: com.demo.service.impl.UploadServiceImpl.java
 * @Description: 上传文件
 * @Author: tanyp
 * @Date: 2022/12/19 15:18
 **/
@Slf4j
@Service
public 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;

/**
 * @ClassName: FtpConfig.java
 * @ClassPath: com.demo.config.FtpConfig.java
 * @Description: FTP配置
 * @Author: tanyp
 * @Date: 2022/12/19 22:28
 **/
@Component
public 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

# ftp
ftp:
  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

Configuring Nginx

server {
    listen       8080;
    server_name  localhost;

    location /images/ {
        root  /app/upload/;
        autoindex on;
    }
}

The above is the detailed content of How SpringBoot uses FTP to operate files. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software