隨筆-204  評論-90  文章-8  trackbacks-0
          package ?com.ihandy.shchinamobile.util;

          import ?sun.net.ftp. * ;
          import ?sun.net. * ;
          import ?java.io.FileInputStream;
          import ?java.io.FileOutputStream;
          import ?java.io.ByteArrayOutputStream;
          import ?java.util.ArrayList;
          import ?java.util.StringTokenizer;

          /**
          ????FTP遠程命令列表<br>
          USER????PORT????RETR????ALLO????DELE????SITE????XMKD????CDUP????FEAT<br>
          PASS????PASV????STOR????REST????CWD?????STAT????RMD?????XCUP????OPTS<br>
          ACCT????TYPE????APPE????RNFR????XCWD????HELP????XRMD????STOU????AUTH<br>
          REIN????STRU????SMNT????RNTO????LIST????NOOP????PWD?????SIZE????PBSZ<br>
          QUIT????MODE????SYST????ABOR????NLST????MKD?????XPWD????MDTM????PROT<br>
          ?????在服務器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上\r\n<br>
          ??????????ftpclient.sendServer("XMKD?/test/bb\r\n");?//執行服務器上的FTP命令<br>
          ??????????ftpclient.readServerResponse一定要在sendServer后調用<br>
          ??????????nameList("/test")獲取指目錄下的文件列表<br>
          ??????????XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯<br>
          ??????????XRMD刪除目錄<br>
          ??????????DELE刪除文件<br>
          *?<p>Title:?使用JAVA操作FTP服務器(FTP客戶端)</p>
          *?<p>Description:?上傳文件的類型及文件大小都放到調用此類的方法中去檢測,比如放到前臺JAVASCRIPT中去檢測等
          *?針對FTP中的所有調用使用到文件名的地方請使用完整的路徑名(絕對路徑開始)。
          *?</p>
          *?<p>Copyright:?Copyright?(c)?2005</p>
          *?<p>Company:?靜靖工作室</p>
          *?
          @author ?歐朝敬??13873195792
          *?
          @version ?1.0
          */


          public ? class ?FTPConnectorBK? {
          ????
          private ?FtpClient?ftpclient;
          ????
          private ?String?ipAddress;
          ????
          private ? int ?ipPort;
          ????
          private ?String?userName;
          ????
          private ?String?PassWord;
          ????
          /**
          ?????*?構造函數
          ?????*?
          @param ?ip?String?機器IP
          ?????*?
          @param ?port?String?機器FTP端口號
          ?????*?
          @param ?username?String?FTP用戶名
          ?????*?
          @param ?password?String?FTP密碼
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ?FTPConnectorBK(String?ip,? int ?port,?String?username,?String?password)? throws
          ????????????Exception?
          {
          ????????ipAddress?
          = ? new ?String(ip);
          ????????ipPort?
          = ?port;
          ????????ftpclient?
          = ? new ?FtpClient(ipAddress,?ipPort);
          ????????
          // ftpclient?=?new?FtpClient(ipAddress);
          ????????userName? = ? new ?String(username);
          ????????PassWord?
          = ? new ?String(password);
          ????}


          ????
          /**
          ?????*?構造函數
          ?????*?
          @param ?ip?String?機器IP,默認端口為21
          ?????*?
          @param ?username?String?FTP用戶名
          ?????*?
          @param ?password?String?FTP密碼
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ?FTPConnectorBK(String?ip,?String?username,?String?password)? throws
          ????????????Exception?
          {
          ????????ipAddress?
          = ? new ?String(ip);
          ????????ipPort?
          = ? 21 ;
          ????????ftpclient?
          = ? new ?FtpClient(ipAddress,?ipPort);
          ????????
          // ftpclient?=?new?FtpClient(ipAddress);
          ????????userName? = ? new ?String(username);
          ????????PassWord?
          = ? new ?String(password);
          ????}



          ????
          /**
          ?????*?登錄FTP服務器
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?login()? throws ?Exception? {
          ????????ftpclient.login(userName,?PassWord);
          ????}


          ????
          /**
          ?????*?退出FTP服務器
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?logout()? throws ?Exception? {
          ????????
          // 用ftpclient.closeServer()斷開FTP出錯時用下更語句退出
          ????????ftpclient.sendServer( " QUIT\r\n " );
          ????????
          int ?reply? = ?ftpclient.readServerResponse();? // 取得服務器的返回信息
          ????}


          ????
          /**
          ?????*?在FTP服務器上建立指定的目錄,當目錄已經存在的情下不會影響目錄下的文件,這樣用以判斷FTP
          ?????*?上傳文件時保證目錄的存在目錄格式必須以"/"根目錄開頭
          ?????*?
          @param ?pathList?String
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?buildList(String?pathList)? throws ?Exception? {
          ????????ftpclient.ascii();
          ????????StringTokenizer?s?
          = ? new ?StringTokenizer(pathList,? " / " );? // sign
          ???????? int ?count? = ?s.countTokens();
          ????????String?pathName?
          = ? "" ;
          ????????
          while ?(s.hasMoreElements())? {
          ????????????pathName?
          = ?pathName? + ? " / " ? + ?(String)?s.nextElement();
          ????????????
          try ? {
          ????????????????ftpclient.sendServer(
          " XMKD? " ? + ?pathName? + ? " \r\n " );
          ????????????}
          ? catch ?(Exception?e)? {
          ????????????????e?
          = ? null ;
          ????????????}

          ????????????
          int ?reply? = ?ftpclient.readServerResponse();
          ????????}

          ????????ftpclient.binary();
          ????}


          ????
          /**
          ?????*?取得指定目錄下的所有文件名,不包括目錄名稱
          ?????*?分析nameList得到的輸入流中的數,得到指定目錄下的所有文件名
          ?????*?
          @param ?fullPath?String
          ?????*?
          @return ?ArrayList
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ?ArrayList?fileNames(String?fullPath)? throws ?Exception? {
          ????????ftpclient.ascii();?
          // 注意,使用字符模式
          ????????TelnetInputStream?list? = ?ftpclient.nameList(fullPath);
          ????????
          byte []?names? = ? new ? byte [ 2048 ];
          ????????
          int ?bufsize? = ? 0 ;
          ????????bufsize?
          = ?list.read(names,? 0 ,?names.length);? // 從流中讀取
          ????????list.close();
          ????????ArrayList?namesList?
          = ? new ?ArrayList();
          ????????
          int ?i? = ? 0 ;
          ????????
          int ?j? = ? 0 ;
          ????????
          while ?(i? < ?bufsize? /* names.length */ )? {
          ????????????
          // char?bc?=?(char)?names;
          ????????????
          // System.out.println(i?+?"??"?+?bc?+?"?:?"?+?(int)?names);
          ????????????
          // i?=?i?+?1;
          ???????????? if ?(names[i]? == ? 10 )? {? // 字符模式為10,二進制模式為13
          ????????????????
          // 文件名在數據中開始下標為j,i-j為文件名的長度,文件名在數據中的結束下標為i-1
          ????????????????
          // System.out.write(names,?j,?i?-?j);
          ????????????????
          // System.out.println(j?+?"???"?+?i?+?"????"?+?(i?-?j));
          ????????????????String?tempName? = ? new ?String(names,?j,?i? - ?j);
          ????????????????namesList.add(tempName);
          ????????????????
          // System.out.println(temp);
          ????????????????
          // ?處理代碼處
          ????????????????
          // j?=?i?+?2;? // 上一次位置二進制模式
          ????????????????j? = ?i? + ? 1 ;? // 上一次位置字符模式
          ????????????}

          ????????????i?
          = ?i? + ? 1 ;
          ????????}

          ????????
          return ?namesList;
          ????}


          ????
          /**
          ?????*?上傳文件到FTP服務器,destination路徑以FTP服務器的"/"開始,帶文件名、
          ?????*?上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
          ?????*?
          @param ?source?String
          ?????*?
          @param ?destination?String
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?upFile(String?source,?String?destination)? throws ?Exception? {
          ????????buildList(destination.substring(
          0 ,?destination.lastIndexOf( " / " )));
          ????????ftpclient.binary();?
          // 此行代碼必須放在buildList之后
          ????????TelnetOutputStream?ftpOut? = ?ftpclient.put(destination);
          ????????TelnetInputStream?ftpIn?
          = ? new ?TelnetInputStream( new
          ????????????????FileInputStream(source),?
          true );
          ????????
          byte []?buf? = ? new ? byte [ 204800 ];
          ????????
          int ?bufsize? = ? 0 ;
          ????????
          while ?((bufsize? = ?ftpIn.read(buf,? 0 ,?buf.length))? != ? - 1 )? {
          ????????????ftpOut.write(buf,?
          0 ,?bufsize);
          ????????}

          ????????ftpIn.close();
          ????????ftpOut.close();

          ????}



          ????
          /**
          ?????*?JSP中的流上傳到FTP服務器,
          ?????*?上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
          ?????*?字節數組做為文件的輸入流,此方法適用于JSP中通過
          ?????*?request輸入流來直接上傳文件在RequestUpload類中調用了此方法,
          ?????*?destination路徑以FTP服務器的"/"開始,帶文件名
          ?????*?
          @param ?sourceData?byte[]
          ?????*?
          @param ?destination?String
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?upFile( byte []?sourceData,?String?destination)? throws ?Exception? {
          ????????buildList(destination.substring(
          0 ,?destination.lastIndexOf( " / " )));
          ????????ftpclient.binary();?
          // 此行代碼必須放在buildList之后
          ????????TelnetOutputStream?ftpOut? = ?ftpclient.put(destination);
          ????????ftpOut.write(sourceData,?
          0 ,?sourceData.length);
          // ????????ftpOut.flush();
          ????????ftpOut.close();
          ????}


          ????
          /**
          ?????*?從FTP文件服務器上下載文件SourceFileName,到本地destinationFileName
          ?????*?所有的文件名中都要求包括完整的路徑名在內
          ?????*?
          @param ?SourceFileName?String
          ?????*?
          @param ?destinationFileName?String
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? void ?downFile(String?SourceFileName,?String?destinationFileName)? throws
          ????????????Exception?
          {
          ????????ftpclient.binary();?
          // 一定要使用二進制模式
          ????????TelnetInputStream?ftpIn? = ?ftpclient.get(SourceFileName);
          ????????
          byte []?buf? = ? new ? byte [ 204800 ];
          ????????
          int ?bufsize? = ? 0 ;
          ????????FileOutputStream?ftpOut?
          = ? new ?FileOutputStream(destinationFileName);
          ????????
          while ?((bufsize? = ?ftpIn.read(buf,? 0 ,?buf.length))? != ? - 1 )? {
          ????????????ftpOut.write(buf,?
          0 ,?bufsize);
          ????????}

          ????????ftpOut.close();
          ????????ftpIn.close();
          ????}


          ????
          /**
          ?????*從FTP文件服務器上下載文件,輸出到字節數組中
          ?????*?
          @param ?SourceFileName?String
          ?????*?
          @return ?byte[]
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? byte []?downFile(String?SourceFileName)? throws
          ????????????Exception?
          {
          ????????ftpclient.binary();?
          // 一定要使用二進制模式
          ????????TelnetInputStream?ftpIn? = ?ftpclient.get(SourceFileName);
          ????????ByteArrayOutputStream?byteOut?
          = ? new ?ByteArrayOutputStream();
          ????????
          byte []?buf? = ? new ? byte [ 204800 ];
          ????????
          int ?bufsize? = ? 0 ;

          ????????
          while ?((bufsize? = ?ftpIn.read(buf,? 0 ,?buf.length))? != ? - 1 )? {
          ????????????byteOut.write(buf,?
          0 ,?bufsize);
          ????????}

          ????????
          byte []?return_arraybyte? = ?byteOut.toByteArray();
          ????????byteOut.close();
          ????????ftpIn.close();
          ????????
          return ?return_arraybyte;
          ????}


          ????
          /** 調用示例
          ?????*?FtpUpfile?fUp?=?new?FtpUpfile("192.150.189.22",?21,?"admin",?"admin");
          ?????*?fUp.login();
          ?????*?fUp.buildList("/adfadsg/sfsdfd/cc");
          ?????*?String?destination?=?"/test.zip";
          ?????*?fUp.upFile("C:\\Documents?and?Settings\\Administrator\\My?Documents\\sample.zip",destination);
          ?????*?ArrayList?filename?=?fUp.fileNames("/");
          ?????*?for?(int?i?=?0;?i?<?filename.size();?i++)?{
          ?????*?????System.out.println(filename.get(i).toString());
          ?????*?}
          ?????*?fUp.logout();
          ?????*?
          @param ?args?String[]
          ?????*?
          @throws ?Exception
          ?????
          */

          ????
          public ? static ? void ?main(String[]?args)? throws ?Exception? {
          ????????FTPConnector?fUp?
          = ? new ?FTPConnector( " 218.206.76.252 " ,? 21 ,? " ftpuser " ,? " abc123 " );
          ????????fUp.login();
          ????????
          /* ????????fUp.buildList("/adfadsg/sfsdfd/cc");
          ????????????????String?destination?=?"/test/SetupDJ.rar";
          ????????????????fUp.upFile(
          ?????????"C:\\Documents?and?Settings\\Administrator\\My?Documents\\SetupDJ.rar",
          ????????????????????????destination);
          ????????????????ArrayList?filename?=?fUp.fileNames("/");
          ????????????????for?(int?i?=?0;?i?<?filename.size();?i++)?{
          ????????????????????System.out.println(filename.get(i).toString());
          ????????????????}

          ????????????????fUp.downFile("/sample.zip",?"d:\\sample.zip");
          ?????????
          */

          ????????FileInputStream?fin?
          = ? new ?FileInputStream(
          ????????????????
          " d:\\OrdSub_20061115133821_401517.xml " );
          ????????
          byte []?data? = ? new ? byte [ 20480000 ];
          ????????fin.read(data,?
          0 ,?data.length);
          ????????fUp.upFile(
          " d:/OrdSub_20061115133821_401517.xml " ,? " /OrdSub_20061115133821_401517.xml " );
          ????????fUp.logout();
          ????????System.out.println(
          " 程序運行完成! " );
          ????????
          /* FTP遠程命令列表
          ?????????USER????PORT????RETR????ALLO????DELE????SITE????XMKD????CDUP????FEAT
          ?????????PASS????PASV????STOR????REST????CWD?????STAT????RMD?????XCUP????OPTS
          ?????????ACCT????TYPE????APPE????RNFR????XCWD????HELP????XRMD????STOU????AUTH
          ?????????REIN????STRU????SMNT????RNTO????LIST????NOOP????PWD?????SIZE????PBSZ
          ?????????QUIT????MODE????SYST????ABOR????NLST????MKD?????XPWD????MDTM????PROT
          ?????????
          */

          ????????
          /* 在服務器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上\r\n
          ?????????ftpclient.sendServer("XMKD?/test/bb\r\n");?//執行服務器上的FTP命令
          ?????????ftpclient.readServerResponse一定要在sendServer后調用
          ?????????nameList("/test")獲取指目錄下的文件列表
          ?????????XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯
          ?????????XRMD刪除目錄
          ?????????DELE刪除文件
          ?????????
          */

          ????}

          }

          posted on 2006-11-27 10:57 一凡 閱讀(4732) 評論(7)  編輯  收藏 所屬分類: JAVA 基礎

          評論:
          # re: java FTP 操作(摘錄) 2007-11-15 14:23 | dxadnwfn
          詳細...謝謝  回復  更多評論
            
          # re: java FTP 操作(摘錄) 2008-03-11 00:52 | ghostwolf
          太感謝你了  回復  更多評論
            
          # re: java FTP 操作(摘錄) 2008-05-15 09:58 | thanks
          非常感謝  回復  更多評論
            
          # re: java FTP 操作(摘錄) 2008-06-19 10:50 | djava
          謝謝咯!正需要這些  回復  更多評論
            
          # re: java FTP 操作(摘錄)[未登錄] 2008-07-17 14:54 | bill
          謝謝了,  回復  更多評論
            
          # re: java FTP 操作(摘錄) 2008-08-14 19:35 | ss
          很好 謝了啊
            回復  更多評論
            
          # re: java FTP 操作(摘錄) 2011-09-02 13:55 | java 新生
          很好多謝了  回復  更多評論
            
          主站蜘蛛池模板: 宁明县| 石阡县| 阿尔山市| 定西市| 九江县| 静宁县| 买车| 镇坪县| 文安县| 九龙城区| 平罗县| 江安县| 青浦区| 朝阳县| 德令哈市| 广安市| 锡林浩特市| 财经| 大兴区| 永川市| 房山区| 嘉鱼县| 雅安市| 吉安县| 万山特区| 淳化县| 龙江县| 丰顺县| 宜州市| 琼中| 庄河市| 万年县| 南陵县| 璧山县| 封丘县| 平和县| 浦江县| 玉门市| 水城县| 自治县| 林州市|