我的java天地

          java對ftp的操作

          ?? 由于項目需要用到對FTP上的文件進行上傳下載等操作,先學(xué)習(xí)了一下。比較簡單,下面來記錄下步驟
          一.先在另外機器上面安裝一個FTP服務(wù)器,我選擇的是SERV-U。這個比較好。先安裝,然后設(shè)置FTP的IP,目錄,用戶名,密碼,權(quán)限等,通過管理界面能很快速的進行配置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. ???????????? //設(shè)置上傳目錄? ??
          32. ????????????ftpClient.changeWorkingDirectory( "/home/oracle" );? ??
          33. ????????????ftpClient.setBufferSize( 1024 );? ??
          34. ????????????ftpClient.setControlEncoding( "GBK" );? ??
          35. ???????????? //設(shè)置文件類型(二進制)? ??
          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( "關(guān)閉FTP連接發(fā)生異常!" ,?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. ???????????? //設(shè)置文件類型(二進制)? ??
          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( "關(guān)閉FTP連接發(fā)生異常!" ,?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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鹤庆县| 兴隆县| 固始县| 潜山县| 岢岚县| 上饶县| 安化县| 大田县| 北安市| 东至县| 岢岚县| 江川县| 沾化县| 开封县| 太仆寺旗| 宁德市| 泰州市| 锦州市| 陇西县| 长顺县| 内黄县| 岳普湖县| 昭平县| 和田县| 塔河县| 罗山县| 康定县| 海原县| 即墨市| 台江县| 凉山| 唐山市| 龙泉市| 三门县| 图们市| 合川市| 沂水县| 广昌县| 额尔古纳市| 婺源县| 博客|