<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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>
    ?????在服務(wù)器上執(zhí)行命令,如果用sendServer來執(zhí)行遠程命令(不能執(zhí)行本地FTP命令)的話,所有FTP命令都要加上\r\n<br>
    ??????????ftpclient.sendServer("XMKD?/test/bb\r\n");?//執(zhí)行服務(wù)器上的FTP命令<br>
    ??????????ftpclient.readServerResponse一定要在sendServer后調(diào)用<br>
    ??????????nameList("/test")獲取指目錄下的文件列表<br>
    ??????????XMKD建立目錄,當目錄存在的情況下再次創(chuàng)建目錄時報錯<br>
    ??????????XRMD刪除目錄<br>
    ??????????DELE刪除文件<br>
    *?<p>Title:?使用JAVA操作FTP服務(wù)器(FTP客戶端)</p>
    *?<p>Description:?上傳文件的類型及文件大小都放到調(diào)用此類的方法中去檢測,比如放到前臺JAVASCRIPT中去檢測等
    *?針對FTP中的所有調(diào)用使用到文件名的地方請使用完整的路徑名(絕對路徑開始)。
    *?</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;
    ????
    /**
    ?????*?構(gòu)造函數(shù)
    ?????*?
    @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);
    ????}


    ????
    /**
    ?????*?構(gòu)造函數(shù)
    ?????*?
    @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服務(wù)器
    ?????*?
    @throws ?Exception
    ?????
    */

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


    ????
    /**
    ?????*?退出FTP服務(wù)器
    ?????*?
    @throws ?Exception
    ?????
    */

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


    ????
    /**
    ?????*?在FTP服務(wù)器上建立指定的目錄,當目錄已經(jīng)存在的情下不會影響目錄下的文件,這樣用以判斷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得到的輸入流中的數(shù),得到指定目錄下的所有文件名
    ?????*?
    @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
    ????????????????
    // 文件名在數(shù)據(jù)中開始下標為j,i-j為文件名的長度,文件名在數(shù)據(jù)中的結(jié)束下標為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服務(wù)器,destination路徑以FTP服務(wù)器的"/"開始,帶文件名、
    ?????*?上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
    ?????*?
    @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服務(wù)器,
    ?????*?上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
    ?????*?字節(jié)數(shù)組做為文件的輸入流,此方法適用于JSP中通過
    ?????*?request輸入流來直接上傳文件在RequestUpload類中調(diào)用了此方法,
    ?????*?destination路徑以FTP服務(wù)器的"/"開始,帶文件名
    ?????*?
    @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文件服務(wù)器上下載文件SourceFileName,到本地destinationFileName
    ?????*?所有的文件名中都要求包括完整的路徑名在內(nèi)
    ?????*?
    @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文件服務(wù)器上下載文件,輸出到字節(jié)數(shù)組中
    ?????*?
    @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;
    ????}


    ????
    /** 調(diào)用示例
    ?????*?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
    ?????????
    */

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

    ????}

    }

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

    評論:
    # re: java FTP 操作(摘錄) 2007-11-15 14:23 | dxadnwfn
    詳細...謝謝  回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄) 2008-03-11 00:52 | ghostwolf
    太感謝你了  回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄) 2008-05-15 09:58 | thanks
    非常感謝  回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄) 2008-06-19 10:50 | djava
    謝謝咯!正需要這些  回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄)[未登錄] 2008-07-17 14:54 | bill
    謝謝了,  回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄) 2008-08-14 19:35 | ss
    很好 謝了啊
      回復(fù)  更多評論
      
    # re: java FTP 操作(摘錄) 2011-09-02 13:55 | java 新生
    很好多謝了  回復(fù)  更多評論
      
    主站蜘蛛池模板: 在线免费观看a级片| 国产精品无码免费专区午夜| 亚洲一区中文字幕在线电影网| 亚洲欧洲日产国码久在线观看| 亚洲av无码一区二区三区不卡| 亚洲区小说区激情区图片区| 亚洲精品自在在线观看| 亚洲人成人一区二区三区| 国产成人亚洲综合无码精品 | 日本免费电影一区二区| 国产日韩一区二区三免费高清| 丁香花在线视频观看免费| 久久国产乱子免费精品| 99热在线精品免费播放6| h片在线免费观看| 女人18毛片免费观看| 四虎影在线永久免费观看| 亚洲午夜无码片在线观看影院猛| 国内精品99亚洲免费高清| 国产亚洲综合一区柠檬导航| 亚洲小视频在线观看| 亚洲免费在线视频观看| 亚洲日韩精品无码专区| 杨幂最新免费特级毛片| aaa毛片免费观看| 美丽的姑娘免费观看在线播放| 欧洲黑大粗无码免费| 全部免费国产潢色一级| 亚洲精品自产拍在线观看| 亚洲男人的天堂在线| 亚洲国产成人久久精品大牛影视| 黄色免费网址在线观看| 久久综合九色综合97免费下载| 毛片免费全部播放无码| 午夜神器成在线人成在线人免费| 亚洲精品麻豆av| 久久亚洲sm情趣捆绑调教| 亚洲1区2区3区精华液| 伊人免费在线观看| 欧美三级在线电影免费| 国产成人高清亚洲|