ftp登录命令:
windows环境下登录:文件管理资源的地址栏中输入“ftp://ip:port”,然后根据提示输入账号、密码。linux环境下登录:ftp ip,然后根据提示输入账号、密码,linux中的ftp命令与基本命令的操作一致。
Maven依赖
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency>
Java的ftp上传
@PostConstruct public void init(){ this.ftpClient = FtpUtils.initFtpClient(hostname, port, username ,password); this.ftpClient.setControlEncoding("UTF-8"); this.ftpClient.setConnectTimeout(20000); this.ftpClient.setDataTimeout(600000); this.ftpClient.enterLocalPassiveMode(); this.timeStamp = DateUtil.today(); } public void uploadFtpFiile(){ boolean upload = false; try { InputStream inputStream = new FileInputStream(ftpFileTemplate); FTPClient ftpClient = FtpUtils.getFtpClient(); //每次数据连接之前ftp client告诉ftp server开通 一个端口来传输数据 ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //指定两层目录 upload/xxxx年-xx月-dd日 ftpPath = CommonConfig.get("fpt.path", "upload"); if (!ftpClient.changeWorkingDirectory(ftpPath)){ ftpClient.makeDirectory(ftpPath); } ftpClient.changeWorkingDirectory(ftpPath); if (!ftpClient.changeWorkingDirectory(DateUtil.today())){ ftpClient.makeDirectory(DateUtil.today()); } ftpClient.changeWorkingDirectory(DateUtil.today()); //ftp上传 filename = "ftp.test.file.res"; upload = ftpClient.storeFile(new String(filename.getBytes("GBK"), StandardCharsets.ISO_8859_1), inputStream); inputStream.close(); }catch (FTPConnectionClosedException ftpConnectionClosedException){ log.error("ftp连接异常重新连接:",ftpConnectionClosedException); FtpUtils.init(); } catch (Exception e) { log.error("ftp推送失败:",e); } log.info("ftp upload status:{}",upload); }
Java的ftp下载
@PostConstruct public void init(){ this.ftpClient = FtpUtils.initFtpClient(hostname, port, username ,password); this.ftpClient.setControlEncoding("UTF-8"); this.ftpClient.setConnectTimeout(20000); this.ftpClient.setDataTimeout(600000); this.ftpClient.enterLocalPassiveMode(); this.timeStamp = DateUtil.today(); } public void readFtpFiile() throws Exception { //读取今天生成的文件数据 String today = DateUtil.today().replaceAll("-", ""); ftpClient.changeWorkingDirectory(today); //每次数据连接之前ftp client告诉ftp server开通 一个端口来传输数据 ftpClient.enterLocalPassiveMode(); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile ftpFile : ftpFiles) { String name = ftpFile.getName(); log.info("文件名称:{}",name); //文件名称获取文件流 InputStream inputStream = ftpClient.retrieveFileStream(name); } }
ftp工具类
@Component@Slf4jpublic class FtpUtils { private static FTPClient ftpClient; public static void init(){ FtpUtils.ftpClient = FtpUtils.initFtpClient(CommonConfig.get("ftp.hostname", "172.25.241.191"), CommonConfig.getInteger("ftp.port",21), CommonConfig.getString("ftp.username", "ftpuser") , CommonConfig.getString("ftp.password", "xsZYroD3")); FtpUtils.ftpClient.setControlEncoding("GBK"); FtpUtils.ftpClient.setConnectTimeout(20000); FtpUtils.ftpClient.setDataTimeout(600000); FtpUtils.ftpClient.enterLocalPassiveMode(); } public static FTPClient initFtpClient(String hostname, int port, String username, String password) { FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); try { log.info("connecting...ftp服务器:{},{}",hostname,port); ftpClient.connect(hostname, port); //连接ftp服务器 ftpClient.login(username, password); //登录ftp服务器 int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器 if(!FTPReply.isPositiveCompletion(replyCode)){ log.info("connect failed...ftp服务器:{}/{}",hostname, port); } log.info("connect successful...ftp服务器:{}/{}",hostname,port); }catch (MalformedURLException e) { log.error("连接ftp异常MalformedURLException:",e); }catch (IOException e) { log.error("连接ftp异常IOException:",e); } return ftpClient; } public static FTPClient getFtpClient() { init(); return ftpClient; }}
来源地址:https://blog.csdn.net/qq_52231508/article/details/129663392