java對ftp的操作
?? 由于項目需要用到對FTP上的文件進行上傳下載等操作,先學習了一下。比較簡單,下面來記錄下步驟
一.先在另外機器上面安裝一個FTP服務器,我選擇的是SERV-U。這個比較好。先安裝,然后設置FTP的IP,目錄,用戶名,密碼,權限等,通過管理界面能很快速的進行配置OK
二.需要在工程的類路徑上面加入一個JAR包ftp.jar
三.開始操作FTP上面的文件了
- import ?org.apache.commons.io.IOUtils;? ??
- import ?org.apache.commons.net.ftp.FTPClient;? ??
- ??
- import ?java.io.File;? ??
- import ?java.io.FileInputStream;? ??
- import ?java.io.IOException;? ??
- import ?java.io.FileOutputStream;? ??
- ??
- /**? ?
- *?
- *? ?
- *? */ ? ??
- public ? class ?FtpTest?{? ??
- ???? public ? static ? void ?main(String[]?args)?{? ??
- ????????testUpload();? ??
- ???????? //testDownload();? ??
- ????}? ??
- ??
- ???? /**? ?
- ?????*?FTP上傳單個文件測試? ?
- ?????*/ ? ??
- ???? public ? static ? void ?testUpload()?{? ??
- ????????FTPClient?ftpClient?=? new ?FTPClient();? ??
- ????????FileInputStream?fis?=? null ;? ??
- ??
- ???????? try ?{? ??
- ????????????ftpClient.connect( "172.20.82.227" );? ??
- ????????????ftpClient.login( "oracle" ,? "oracle" );? ??
- ????????????File?srcFile?=? new ?File( "E:/apache+tomcat.zip" );? ??
- ????????????fis?=? new ?FileInputStream(srcFile);? ??
- ???????????? //設置上傳目錄? ??
- ????????????ftpClient.changeWorkingDirectory( "/home/oracle" );? ??
- ????????????ftpClient.setBufferSize( 1024 );? ??
- ????????????ftpClient.setControlEncoding( "GBK" );? ??
- ???????????? //設置文件類型(二進制)? ??
- ????????????ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);? ??
- ????????????ftpClient.storeFile( "apache+tomcat.zip" ,?fis);? ??
- ????????????System.out.println( "成功!" ); ??
- ????????}? catch ?(IOException?e)?{? ??
- ????????????e.printStackTrace();? ??
- ???????????? throw ? new ?RuntimeException( "FTP客戶端出錯!" ,?e);? ??
- ????????}? finally ?{? ??
- ????????????IOUtils.closeQuietly(fis);? ??
- ???????????? try ?{? ??
- ????????????????ftpClient.disconnect();? ??
- ????????????}? catch ?(IOException?e)?{? ??
- ????????????????e.printStackTrace();? ??
- ???????????????? throw ? new ?RuntimeException( "關閉FTP連接發生異常!" ,?e);? ??
- ????????????}? ??
- ????????}? ??
- ????}? ??
- ??
- ???? /**? ?
- ?????*?FTP下載單個文件測試? ?
- ?????*/ ? ??
- ???? public ? static ? void ?testDownload()?{? ??
- ????????FTPClient?ftpClient?=? new ?FTPClient();? ??
- ????????FileOutputStream?fos?=? null ;? ??
- ??
- ???????? try ?{? ??
- ????????????ftpClient.connect( "192.168.14.117" );? ??
- ????????????ftpClient.login( "admin" ,? "123" );? ??
- ??
- ????????????String?remoteFileName?=? "/admin/pic/3.gif" ;? ??
- ????????????fos?=? new ?FileOutputStream( "c:/down.gif" );? ??
- ??
- ????????????ftpClient.setBufferSize( 1024 );? ??
- ???????????? //設置文件類型(二進制)? ??
- ????????????ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);? ??
- ????????????ftpClient.retrieveFile(remoteFileName,?fos);? ??
- ????????}? catch ?(IOException?e)?{? ??
- ????????????e.printStackTrace();? ??
- ???????????? throw ? new ?RuntimeException( "FTP客戶端出錯!" ,?e);? ??
- ????????}? finally ?{? ??
- ????????????IOUtils.closeQuietly(fos);? ??
- ???????????? try ?{? ??
- ????????????????ftpClient.disconnect();? ??
- ????????????}? catch ?(IOException?e)?{? ??
- ????????????????e.printStackTrace();? ??
- ???????????????? throw ? new ?RuntimeException( "關閉FTP連接發生異常!" ,?e);? ??
- ????????????}? ??
- ????????}? ??
- ????} ??
- ???? ??
-
}???
?
?
posted on 2009-07-25 21:45 tobyxiong 閱讀(245) 評論(0) 編輯 收藏 所屬分類: java