Java通过apache的common包操作ftp
场景:
1.获取ftp列表
2.获取文件
实现:
private static void list() { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect("ftp.someabcd.com", 21); ftpClient.setControlEncoding("UTF-8"); ftpClient.login("anonymous", "[email protected]"); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); FTPFile[] ftpFiles = ftpClient.listFiles("/"); for (FTPFile ftpFile : ftpFiles) { System.out.println(ftpFile.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
get文件
private static void get() { FTPClient ftpClient = new FTPClient(); OutputStream is =null; try { ftpClient.connect("ftp.someabcd.com", 21); ftpClient.setControlEncoding("UTF-8"); ftpClient.login("anonymous", "[email protected]"); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); File localFile = new File("G:\\a.txt"); is = new FileOutputStream(localFile); ftpClient.retrieveFile("/temp/128.139.txt", is); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
其他常用操作:
ftpClient.dele(pathname); ftpClient.deleteFile(pathname); ftpClient.storeFile(remote, local);
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01