Java -- ftp 上传工具类

1、文件内容

package com.sample.common.utils;

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

import java.io.*;

public class FtpUtils {

    private static final String FTP_HOME = "/home/ftp";

    /**
     * 连接信息配置
     */
    private String host;
    private int port;
    private String username;
    private String password;

    private FTPClient client;

    public FtpUtils(String host, int port, String username, String password) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;

        openClient();
    }

    /**
     * 打开连接
     */
    private void openClient(){
        try {
            client = new FTPClient();
            //1.设置连接属性
            client.setConnectTimeout(5000);
            client.setControlEncoding("UTF-8");
//            client.enterLocalActiveMode();

            //2.使用 host 和 port 连接服务器,并登陆
            client.connect(this.host, this.port);
            client.login(this.username, this.password);

            //3.检查连接状态
            if(!FTPReply.isPositiveCompletion(client.getReplyCode())){
                System.out.println("ftp 连接失败!");
                client.disconnect();
            }else{
                System.out.println("ftp 连接成功!");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("ftp 连接异常!");
        }
    }

    /**
     * 关闭连接
     */
    public void closeClient(){
        try {
            client.logout();
        }catch (Exception e){
            System.out.println("ftp 关闭失败!");
        }finally {
            if(client.isConnected()){
                try {
                    client.disconnect();
                } catch (IOException e) {
                    System.out.println("ftp 关闭失败!");
                }
            }
        }
    }

    /**
     * 下载文件
     * @param serverPath 文件处于服务器的路径
     * @param savePath   文件保存的本地位置
     * @param fileName   文件的名称
     */
    public boolean downloadFile(String serverPath, String savePath, String fileName){
        boolean downFlag = false;
        try {
            //1.开启被动模式
            client.enterLocalPassiveMode();
            //2.切换目录
            client.changeWorkingDirectory(serverPath);

            //3.查找文件
            for(FTPFile file:client.listFiles()){
                if(fileName.equals(file.getName())){
                    //4.保存路径
                    OutputStream saveFile = new FileOutputStream(new File(savePath + File.separator + fileName));
                    client.retrieveFile(new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"), saveFile);
                    downFlag = true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("下载失败!");
        }
        return downFlag;
    }

    /**
     * 上传文件
     * @param serverPath 文件处于服务器的路径
     * @param filePath   文件保存的本地位置
     */
    public boolean uploadFile(String serverPath, String filePath){
        boolean uploadFlag = false;
        try {
            //1.开启被动模式
            client.enterLocalPassiveMode();
            //2.使用二进制传输方式
            client.setFileType(FTPClient.BINARY_FILE_TYPE);

            //3.创建文件夹
            if(!client.changeWorkingDirectory(serverPath)){
                client.makeDirectory(serverPath);
                client.changeWorkingDirectory(serverPath);
            }

            //4.上传文件
            File file = new File(filePath);
            return client.storeFile(serverPath + file.getName(), new FileInputStream(file));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("上传文件失败!");
        }
        return uploadFlag;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static void main(String[] args) {
        FtpUtils ftp = new FtpUtils("", 21, "", "");
//        boolean flag = ftp.uploadFile(FTP_HOME+"/images/", "F:/ftp/files/temp.jpg");
        boolean flag = ftp.downloadFile(FTP_HOME+"/images/", "F:/", "temp.jpg");
        System.out.println(flag);
    }
}

 

你可能感兴趣的:(Java)