JAVA使用FTP实现文件传输

1.FTP是文件传输协议,可以用来搭建文件服务器,供客户端对文件进行上传和下载的操作

2.JAVA中可以使用commons-net.jar的包,实现对FTP的操作

3.下面是小栗子

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

4.源码:

package edu.net.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * FTP实例
 */
public class FTPDemo {
    public static void main(String[] args) {
        String server = "localhost";
        String username = "caohonghua";
        String password = "******";
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient(server, username, password);
            System.out.println(ftpClient.getReplyString());
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.err.println("连接FTP服务器失败!");
                System.exit(1);
            }
            //获取当前工作空间
            String cwd = ftpClient.printWorkingDirectory();
            System.out.println(cwd);
            System.out.println("--------------");
            //列出当前路径下的文件及其文件夹
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                System.out.println(ftpFile.toString());
            }
            System.out.println("--------------");
            //上传文件
            String source = "/tmp/test";
            String dest = cwd;
            System.out.println(upload(ftpClient, source, dest));
            //下载文件
            source = "/Users/caohonghua/downloadTest";
            dest = "/tmp/downloadTest";
            System.out.println(download(ftpClient, source, dest));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(ftpClient)) {
                try {
                    ftpClient.logout();
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static boolean download(FTPClient ftpClient, String source, String dest) {
        boolean flag = false;
        try (OutputStream outputStream = Files.newOutputStream(Paths.get(dest))) {
            flag = ftpClient.retrieveFile(source, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    private static boolean upload(FTPClient ftpClient, String source, String dest) {
        boolean flag = false;
        Path path = Paths.get(source);
        try (InputStream inputStream = Files.newInputStream(path)) {
            ftpClient.changeWorkingDirectory(dest);
            flag = ftpClient.storeFile(path.getFileName().toString(), inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    private static FTPClient getFTPClient(String server, String username, String password) throws IOException {
        FTPClient ftpClient = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        config.setServerTimeZoneId("Asia/Shanghai");
        ftpClient.configure(config);
        ftpClient.connect(server);
        ftpClient.login(username, password);
        return ftpClient;
    }
}
5.记录:storeFile 存储文件(上传)retrieveFile取回文件(下载)
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页