package com.sinosoft.common.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.io.*; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Month; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; /** * @Author :wjh * @Date :12:17 2019/12/17 **/ @Slf4j public class FtpUtils { public String ftpHost; public int ftpPort; public String ftpUserName; public String ftpPassword; public FtpUtils(String ftpHost,int ftpPort,String ftpUserName,String ftpPassword){ this.ftpHost=ftpHost; this.ftpPort=ftpPort; this.ftpUserName=ftpUserName; this.ftpPassword=ftpPassword; } /** * @Description 连接初始化ftp对象 * @Param [] * @Author wjh * @Date 2019/12/17 12:39 * @exception * @return org.apache.commons.net.ftp.FTPClient **/ 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())) { log.info("未连接到FTP,用户名或密码错误。"); ftpClient.disconnect(); } else { log.info("FTP连接成功。"); } } catch (Exception e) { log.error("FTP连接失败,请正确配置:",e); } return ftpClient; } /** * @Description 下载 * @Param [ftpPath FTP服务器中文件所在路径 格式: ftptest/aa, localPath 下载到本地的位置 格式:H:/download, fileName 文件名称] * @Author wjh * @Date 2019/12/17 12:40 * @exception * @return void **/ public void downloadFtpFile(String ftpPath, String localPath,String fileName) { OutputStream os=null; FTPClient ftpClient=null; try { ftpClient = getFtpClient(); ftpClient.setControlEncoding("UTF-8"); // 中文支持 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(ftpPath); File localFile = new File(localPath + File.separatorChar + fileName); os = new FileOutputStream(localFile); ftpClient.retrieveFile(fileName, os); log.info("FTP下载文件成功!"); } catch (Exception e) { log.error("FTP下载文件失败",e); throw new RuntimeException(e); } finally{ try { os.close(); ftpClient.logout(); } catch (IOException e) { log.error("关闭连接",e); } } } /** * @Description 上传文件 * @Param [ftpPath 服务器文件地址, fileName ftp文件名, input 文件流] * @Author wjh * @Date 2019/12/17 12:42 * @exception * @return boolean **/ public boolean uploadFile(String ftpPath,String fileName,InputStream input) { boolean success = false; FTPClient ftpClient=null; try { int reply; ftpClient = getFtpClient(); reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); return success; } // 中文支持 ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); boolean falg = ftpClient.changeWorkingDirectory(ftpPath); if(falg==false){ ftpClient.makeDirectory(ftpPath); ftpClient.changeWorkingDirectory(ftpPath); } ftpClient.storeFile(fileName, input); success = true; log.info("FTP上传文件成功!"); } catch (IOException e) { success=false; log.error("FTP上传文件失败:",e); } finally { try{ input.close(); }catch (Exception e){} if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) {} } } return success; } }