Java 从FTP下载.上传文件

1.需要导入pom

<dependency>
  <groupId>commons-netgroupId>
   <artifactId>commons-netartifactId>
   <version>3.1version>
dependency>

2.FTP工具类(Ftp IP 端口 用户名 密码都配置在配置文件)

package com.tools.ftp;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.SocketException;

/**
 * @author admin
 * @title: FtpUtils
 * @projectName tools
 * @description: FTP工具类
 * @date 2020/3/1913:23
 */
@Component
public class FtpUtils {

    /**FTP IP*/
    @Value("${ftp.host}")
    private String ftpHost;
    /**FTP用户名*/
    @Value("${ftp.userName}")
    private String ftpUserName;
    /**FTP密码*/
    @Value("${ftp.password}")
    private String ftpPassword;
    /**FTP 端口 默认21*/
    @Value("${ftp.port}")
    private int ftpPort =21;

    public static final String CHARSET_GBK = "GBK";

    public static final String CHARSET_UTF8 = "UTF-8";

    public static final String CHARSET_ISO = "iso-8859-1";

    /**
     * 获取FTPClient
     * @return
     */
    public  FTPClient getFTPClient() {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient = new FTPClient();
            // 连接FTP服务器
            ftpClient.connect(ftpHost, ftpPort);
            // 登陆FTP服务器
            ftpClient.login(ftpUserName, ftpPassword);
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                System.out.println("FTP连接成功。");
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.out.println("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * 下载文件
     * @param ftpPath  ftp文件存放物理路径
     * @param fileName 文件路径
     */
    public  void downloadFile(String ftpPath, String localPath, String fileName) {
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient();
            // 中文支持
            ftpClient.setControlEncoding(CHARSET_UTF8);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(ftpPath);
            File localFile = new File(localPath + File.separatorChar + fileName);
            OutputStream os = new FileOutputStream(localFile);
            ftpClient.retrieveFile(fileName, os);
            os.close();
            ftpClient.logout();
        } catch (FileNotFoundException e) {
            System.out.println("没有找到" + ftpPath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            System.out.println("连接FTP失败.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取错误。");
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     * @param ftpPath  ftp文件存放物理路径
     * @param fileName 文件路径
     * @param input 文件输入流,即从本地服务器读取文件的IO输入流
     */
    public  void uploadFile(String ftpPath,String fileName,InputStream input){
        FTPClient ftp=null;
        try {
            ftp=getFTPClient();
            ftp.changeWorkingDirectory(ftpPath);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            fileName=new String(fileName.getBytes(CHARSET_GBK),CHARSET_ISO);
            ftp.storeFile(fileName, input);
            input.close();
            ftp.logout();
            System.out.println("upload succes!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String getFtpHost() {
        return ftpHost;
    }

    public void setFtpHost(String ftpHost) {
        this.ftpHost = ftpHost;
    }

    public String getFtpUserName() {
        return ftpUserName;
    }

    public void setFtpUserName(String ftpUserName) {
        this.ftpUserName = ftpUserName;
    }

    public String getFtpPassword() {
        return ftpPassword;
    }

    public void setFtpPassword(String ftpPassword) {
        this.ftpPassword = ftpPassword;
    }

    public int getFtpPort() {
        return ftpPort;
    }

    public void setFtpPort(int ftpPort) {
        this.ftpPort = ftpPort;
    }
}

 

你可能感兴趣的:(Java 从FTP下载.上传文件)