我的java天地

          java對ftp的操作

          ?? 由于項目需要用到對FTP上的文件進行上傳下載等操作,先學習了一下。比較簡單,下面來記錄下步驟
          一.先在另外機器上面安裝一個FTP服務器,我選擇的是SERV-U。這個比較好。先安裝,然后設置FTP的IP,目錄,用戶名,密碼,權限等,通過管理界面能很快速的進行配置OK

          二.需要在工程的類路徑上面加入一個JAR包ftp.jar

          三.開始操作FTP上面的文件了

          Java代碼
          1. import ?org.apache.commons.io.IOUtils;? ??
          2. import ?org.apache.commons.net.ftp.FTPClient;? ??
          3. ??
          4. import ?java.io.File;? ??
          5. import ?java.io.FileInputStream;? ??
          6. import ?java.io.IOException;? ??
          7. import ?java.io.FileOutputStream;? ??
          8. ??
          9. /**? ?
          10. *?
          11. *? ?
          12. *? */ ? ??
          13. public ? class ?FtpTest?{? ??
          14. ???? public ? static ? void ?main(String[]?args)?{? ??
          15. ????????testUpload();? ??
          16. ???????? //testDownload();? ??
          17. ????}? ??
          18. ??
          19. ???? /**? ?
          20. ?????*?FTP上傳單個文件測試? ?
          21. ?????*/ ? ??
          22. ???? public ? static ? void ?testUpload()?{? ??
          23. ????????FTPClient?ftpClient?=? new ?FTPClient();? ??
          24. ????????FileInputStream?fis?=? null ;? ??
          25. ??
          26. ???????? try ?{? ??
          27. ????????????ftpClient.connect( "172.20.82.227" );? ??
          28. ????????????ftpClient.login( "oracle" ,? "oracle" );? ??
          29. ????????????File?srcFile?=? new ?File( "E:/apache+tomcat.zip" );? ??
          30. ????????????fis?=? new ?FileInputStream(srcFile);? ??
          31. ???????????? //設置上傳目錄? ??
          32. ????????????ftpClient.changeWorkingDirectory( "/home/oracle" );? ??
          33. ????????????ftpClient.setBufferSize( 1024 );? ??
          34. ????????????ftpClient.setControlEncoding( "GBK" );? ??
          35. ???????????? //設置文件類型(二進制)? ??
          36. ????????????ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);? ??
          37. ????????????ftpClient.storeFile( "apache+tomcat.zip" ,?fis);? ??
          38. ????????????System.out.println( "成功!" ); ??
          39. ????????}? catch ?(IOException?e)?{? ??
          40. ????????????e.printStackTrace();? ??
          41. ???????????? throw ? new ?RuntimeException( "FTP客戶端出錯!" ,?e);? ??
          42. ????????}? finally ?{? ??
          43. ????????????IOUtils.closeQuietly(fis);? ??
          44. ???????????? try ?{? ??
          45. ????????????????ftpClient.disconnect();? ??
          46. ????????????}? catch ?(IOException?e)?{? ??
          47. ????????????????e.printStackTrace();? ??
          48. ???????????????? throw ? new ?RuntimeException( "關閉FTP連接發生異常!" ,?e);? ??
          49. ????????????}? ??
          50. ????????}? ??
          51. ????}? ??
          52. ??
          53. ???? /**? ?
          54. ?????*?FTP下載單個文件測試? ?
          55. ?????*/ ? ??
          56. ???? public ? static ? void ?testDownload()?{? ??
          57. ????????FTPClient?ftpClient?=? new ?FTPClient();? ??
          58. ????????FileOutputStream?fos?=? null ;? ??
          59. ??
          60. ???????? try ?{? ??
          61. ????????????ftpClient.connect( "192.168.14.117" );? ??
          62. ????????????ftpClient.login( "admin" ,? "123" );? ??
          63. ??
          64. ????????????String?remoteFileName?=? "/admin/pic/3.gif" ;? ??
          65. ????????????fos?=? new ?FileOutputStream( "c:/down.gif" );? ??
          66. ??
          67. ????????????ftpClient.setBufferSize( 1024 );? ??
          68. ???????????? //設置文件類型(二進制)? ??
          69. ????????????ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);? ??
          70. ????????????ftpClient.retrieveFile(remoteFileName,?fos);? ??
          71. ????????}? catch ?(IOException?e)?{? ??
          72. ????????????e.printStackTrace();? ??
          73. ???????????? throw ? new ?RuntimeException( "FTP客戶端出錯!" ,?e);? ??
          74. ????????}? finally ?{? ??
          75. ????????????IOUtils.closeQuietly(fos);? ??
          76. ???????????? try ?{? ??
          77. ????????????????ftpClient.disconnect();? ??
          78. ????????????}? catch ?(IOException?e)?{? ??
          79. ????????????????e.printStackTrace();? ??
          80. ???????????????? throw ? new ?RuntimeException( "關閉FTP連接發生異常!" ,?e);? ??
          81. ????????????}? ??
          82. ????????}? ??
          83. ????} ??
          84. ???? ??
          85. }???

          ?

          posted on 2009-07-25 21:45 tobyxiong 閱讀(245) 評論(0)  編輯  收藏 所屬分類: java

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 广饶县| 阿巴嘎旗| 平昌县| 柏乡县| 安化县| 辉南县| 华池县| 神池县| 宣汉县| 盐津县| 承德县| 万全县| 综艺| 隆尧县| 阿克苏市| 肃北| 鄢陵县| 溧水县| 藁城市| 罗平县| 宜城市| 连江县| 宝应县| 木里| 延津县| 奉贤区| 天柱县| 朝阳县| 宜良县| 抚顺县| 德格县| 佛学| 阿城市| 崇州市| 峨边| 旬邑县| 武威市| 晴隆县| 宜城市| 大连市| 军事|