spring boot 连接FTP实现文件上传

发布于:2025-03-01 ⋅ 阅读:(19) ⋅ 点赞:(0)

spring boot 连接FTP实现文件上传

maven:

        <!--ftp-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
        </dependency>

接口示例:

    @ApiOperation(value="上传文件到FTP")
    @PostMapping("/fileUploadFTP")
    public Boolean uploadFileToFtp(@RequestParam("file") MultipartFile file) {
            boolean flag = uploadFile("192.168.2.128",21,file,"user","123456","/");
            return false;
    }

方法:

public static boolean uploadFile(String url, int port, MultipartFile file,String username,
                                     String password, String path) {
        FTPClient ftpClient = new FTPClient();
        boolean result = false;

        try {
            String filename = file.getOriginalFilename();
            // 连接到FTP服务器
            ftpClient.connect(url, port);

            // 登录
            boolean login = ftpClient.login(username, password);
            if (!login) {
                System.out.println("FTP 登录失败");
                return result;
            }

            // 设置控制编码(处理中文文件名)
            ftpClient.setControlEncoding("UTF-8");

            // 检查是否成功连接
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                System.out.println("连接失败,FTP 服务器返回错误:" + replyCode);
                return result;
            }

            // 切换到指定的目录
            boolean changeDir = ftpClient.changeWorkingDirectory(path);
            if (!changeDir) {
                System.out.println("目标目录不存在或没有权限:" + path);
                // 尝试创建目标目录
                if (ftpClient.makeDirectory(path)) {
                    System.out.println("创建目录成功:" + path);
                } else {
                    System.out.println("无法创建目录:" + path);
                    return false;
                }
            }

            // 设置文件类型(二进制文件类型,适用于所有类型的文件)
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            try (InputStream fis = file.getInputStream()) {
                // 上传文件
                String encodedFilename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
                result = ftpClient.storeFile(encodedFilename, fis);
                if (result) {
                    System.out.println("文件上传成功!");
                } else {
                    replyCode = ftpClient.getReplyCode();
                    String replyMessage = ftpClient.getReplyString();
                    System.out.println("上传失败,FTP 回复: " + replyCode + " " + replyMessage);
                }
            }
            // 退出
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 断开与服务器的连接
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

网站公告

今日签到

点亮在社区的每一天
去签到