java:ftp文件上传下载新建文件夹删除文件加入log(一)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ftp")
public class FtpFileTransferController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FtpFileTransferController.class);

    @PostMapping("/login")
    public ResponseEntity<FTPClient> loginToFtp(String server, int port, String user, String pass) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            LOGGER.info("用户 {} 登录 FTP 服务器", user);
            return new ResponseEntity<>(ftpClient, HttpStatus.OK);
        } catch (IOException e) {
            LOGGER.error("用户 {} 连接或登录 FTP 服务器时出现错误: {}", user, e.getMessage());
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/download")
    public ResponseEntity<String> downloadFile(FTPClient ftpClient, String remotePath, String localPath, String operator) {
        try {
            FTPFile[] files = ftpClient.listFiles(remotePath);
            if (files.length > 0) {
                FTPFile file = files[0];
                String originalFileName = file.getName();  // 获取原文件名

                // 检查本地是否存在同名文件,如果存在则添加数字后缀避免冲突
                File localFile = getUniqueLocalFile(originalFileName, localPath);
                OutputStream outputStream = new FileOutputStream(localFile);
                try {
                    ftpClient.retrieveFile(remotePath, outputStream);
                    LOGGER.info("用户 {} 下载文件 {}", operator, remotePath);
                    return new ResponseEntity<>("下载成功", HttpStatus.OK);
                } catch (IOException e) {
                    LOGGER.error("用户 {} 下载文件 {} 时出现错误: {}", operator, remotePath, e.getMessage());
                    return new ResponseEntity<>("下载失败", HttpStatus.INTERNAL_SERVER_ERROR);
                } finally {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        LOGGER.error("用户 {} 关闭输出流时出现错误: {}", operator, e.getMessage());
                    }
                }
            }
            LOGGER.warn("用户 {} 尝试下载文件,但远程路径 {} 下无文件可下载", operator, remotePath);
            return new ResponseEntity<>("远程路径下无文件可下载", HttpStatus.NOT_FOUND);
        } catch (IOException e) {
            LOGGER.error("用户 {} 下载文件操作时出现错误: {}", operator, e.getMessage());
            return new ResponseEntity<>("下载失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(FTPClient ftpClient, String localPath, String remotePath, String operator) {
        try {
            File localFile = new File(localPath);
            String originalFileName = localFile.getName();  // 获取原文件名

            // 检查服务器端是否存在同名文件,如果存在则添加数字后缀避免冲突
            String uniqueFileName = getUniqueRemoteFileName(originalFileName, remotePath, ftpClient);
            FileInputStream inputStream = new FileInputStream(localFile);
            try {
                ftpClient.storeFile(remotePath + File.separator + uniqueFileName, inputStream);
                LOGGER.info("用户 {} 上传文件 {}", operator, localPath);
                return new ResponseEntity<>("上传成功", HttpStatus.OK);
            } catch (IOException e) {
                LOGGER.error("用户 {} 上传文件 {} 时出现错误: {}", operator, localPath, e.getMessage());
                return new ResponseEntity<>("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOGGER.error("用户 {} 关闭输入流时出现错误: {}", operator, e.getMessage());
                }
            }
        } catch (IOException e) {
            LOGGER.error("用户 {} 上传文件操作时出现错误: {}", operator, e.getMessage());
            return new ResponseEntity<>("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/createFolder")
    public ResponseEntity<String> createFolder(FTPClient ftpClient, String folderPath, String operator) {
        try {
            boolean created = ftpClient.makeDirectory(folderPath);
            if (created) {
                LOGGER.info("用户 {} 创建文件夹 {}", operator, folderPath);
                return new ResponseEntity<>("文件夹创建成功", HttpStatus.OK);
            } else {
                LOGGER.error("用户 {} 创建文件夹 {} 失败", operator, folderPath);
                return new ResponseEntity<>("创建文件夹失败", HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            LOGGER.error("用户 {} 创建文件夹 {} 时出现错误: {}", operator, folderPath, e.getMessage());
            return new ResponseEntity<>("创建文件夹失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/deleteFile")
    public ResponseEntity<String> deleteFile(FTPClient ftpClient, String filePath, String operator) {
        try {
            boolean deleted = ftpClient.deleteFile(filePath);
            if (deleted) {
                LOGGER.info("用户 {} 删除文件 {}", operator, filePath);
                return new ResponseEntity<>("文件删除成功", HttpStatus.OK);
            } else {
                LOGGER.error("用户 {} 删除文件 {} 失败", operator, filePath);
                return new ResponseEntity<>("文件删除失败", HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            LOGGER.error("用户 {} 删除文件 {} 时出现错误: {}", operator, filePath, e.getMessage());
            retur

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值