bxqybxqy 2013-03-21
场景:
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);