FTP工具类

mac2024-11-06  11

public class FtpUtil { private static Logger logger = Logger.getLogger(FtpUtil.class); private static String host; //主机 private static String username; //用户名 private static String password; //密码 private static int port; //端口 private FtpUtil() {} /** * 获取ftp实例 * * @param host * @param username * @param password * @param port */ public static FTPClient getFTPClient(String host, String username, String password, String port) { host = ftpHost; username = ftpUserName; password = ftpPassword; port = ftpPort; FTPClient ftpClient = null; try { ftpClient = new FTPClient(); ftpClient.connect(host, port);// 连接FTP服务器 ftpClient.login(username, password);// 登陆FTP服务器 if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { logger.info("未连接到FTP,用户名或密码错误。"); ftpClient.disconnect(); } else { logger.info("FTP连接成功。"); } } catch (SocketException e) { e.printStackTrace(); logger.error("FTP的IP地址可能错误,请正确配置。"); } catch (IOException e) { e.printStackTrace(); logger.error("FTP的端口错误,请正确配置。"); } return ftpClient; } /** * ftp下载到本地 * * @param remotePath ftp路径 * @param localPath 本地路径 * @param fileName 文件名 */ public static void downloadFile(String remotePath, String localPath, String fileName) { FTPClient ftpClient = null; OutputStream os = null; try { ftpClient = getFTPClient(host, username, password, port); ftpClient.setControlEncoding("UTF-8"); // 中文支持 ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(remotePath); os = new FileOutputStream(new File(localPath + "/" + fileName)); ftpClient.retrieveFile(fileName, os); } catch (FileNotFoundException e) { logger.error("没有找到" + remotePath + "文件"); e.printStackTrace(); } catch (SocketException e) { logger.error("连接FTP失败."); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); logger.error("文件读取错误。"); e.printStackTrace(); } finally { try { os.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } } } /** * 上传文件到ftp * * @param remotePath ftp路径 * @param localPath 本地路径 * @param fileName 文件名 */ public static void uploadFile(String remotePath, String localPath, String filename) { InputStream input = null; FTPClient ftpClient = null; try { input = new FileInputStream(localPath + "\\" + filename); ftpClient = getFTPClient(host, username, password, port); ftpClient.changeWorkingDirectory(remotePath); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); filename = new String(filename.getBytes("GBK"), "iso-8859-1"); ftpClient.storeFile(filename, input); input.close(); ftpClient.logout(); System.out.println("upload succes!"); } catch (Exception e) { e.printStackTrace(); } } /** * ftp之间文件传输 * * @param midFtpPath ftp接收端路径 * @param destFtpPath ftp发送端路径 */ public static void ftpToFtp(String midFtpPath, String destFtpPath) { logger.info("连接ftp"); FTPClient midFtpClient = getFTPClient(host, username, password, port); FTPClient destFtpClient = getFTPClient(host, username, password, port); OutputStream os = null; try { midFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE); destFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE); midFtpClient.enterLocalPassiveMode(); destFtpClient.enterLocalPassiveMode(); logger.info("获取输出流"); os = destFtpClient.storeFileStream(destFtpPath); logger.info("拷贝文件到目标ftp"); midFtpClient.retrieveFile(midFtpPath, os); } catch (IOException e) { e.printStackTrace(); } finally { try { logger.info("关闭流"); os.close(); midFtpClient.logout(); logger.info("已注销midFtpClient"); destFtpClient.logout(); logger.info("已注销destFtpClient"); } catch (IOException e) { e.printStackTrace(); } } } }

 

最新回复(0)