【Java/FTP】使用apache.commons的FTPClient往FTP服务器上传下载文件

【FTPClient的依赖】

        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

【代码】

package com.hy.lab.ftp;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FtpUtil {
    /**
     * 上传文件到FTP服务器
     * @param hostname
     * @param port
     * @param username
     * @param password
     * @param locaFilename
     * @param remoteFilename  若只指定文件名,文件将传到用户的缺省目录
     * @return
     * @throws Exception
     */
    public static boolean uploadFileToServer(String hostname,int port,String username,String password,String locaFilename,String remoteFilename) throws Exception{
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.connect(hostname,port);
        ftpClient.login(username, password);
        int replyCode = ftpClient.getReplyCode();
        ftpClient.setDataTimeout(120000);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置为二进制文件

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpClient.disconnect();
            System.out.println("FTP连接失败");
            return false;
        }else {
            System.out.println("FTP连接成功");
        }

        // 本地文件流
        InputStream in = new FileInputStream(locaFilename);
        // 服务器上文件
        String fileOnServer = new String(remoteFilename.getBytes("UTF-8"),"iso-8859-1");

        // 正式上传
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        boolean result=ftpClient.storeFile(fileOnServer, in);
        String msg=String.format("文件%s上传%s",remoteFilename,result?"成功":"失败");
        System.out.println(msg);

        //关闭文件流
        in.close();
        //关闭连接
        if (ftpClient != null) {
            ftpClient.logout();
            ftpClient.disconnect();
        }

        return result;
    }

    /**
     * 从服务器下载文件
     * @param hostname
     * @param port
     * @param username
     * @param password
     * @param locaFilename
     * @param remoteFilename
     * @return
     * @throws Exception
     */
    public static boolean downloadFromServer(String hostname,int port,String username,String password,String locaFilename,String remoteFilename) throws Exception{
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.connect(hostname,port);
        ftpClient.login(username, password);
        int replyCode = ftpClient.getReplyCode();
        ftpClient.setDataTimeout(120000);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置为二进制文件

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftpClient.disconnect();
            System.out.println("FTP连接失败");
            return true;
        }else {
            System.out.println("FTP连接成功");
        }

        File file=new File(locaFilename);
        FileOutputStream fos=new FileOutputStream(file);
        boolean result = ftpClient.retrieveFile(remoteFilename,fos);
        String msg=String.format("文件%s下载%s",locaFilename,result?"成功":"失败");
        System.out.println(msg);

        //关闭文件流
        fos.close();
        //关闭连接
        if (ftpClient != null) {
            ftpClient.logout();
            ftpClient.disconnect();
        }

        return result;
    }

    public static void main(String[] args) throws Exception{
        uploadFileToServer("192.168.111.128",21,"hy","123456","c:\\temp\\local.csv","remote.csv");

        downloadFromServer("192.168.111.128",21,"hy","123456","c:\\temp\\local.csv","remote.csv");
    }
}

【参考资料】

https://blog.csdn.net/fengsheng5210/article/details/78141122

END

posted @ 2022-03-21 14:02  逆火狂飙  阅读(651)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东