首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Java透过apache的common包操作ftp

2013-03-25 
Java通过apache的common包操作ftp场景:1.获取ftp列表2.获取文件实现:private static void list() {FTPClie

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", "anonymous@ftp.someabcd.com");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", "anonymous@ftp.someabcd.com");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);

?

热点排行