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

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

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

    posts - 11,comments - 17,trackbacks - 0
    import ?java.io.InputStream;
    import ?java.util.Properties;

    import ?org.apache.commons.net.ftp.FTPClient;
    import ?org.apache.commons.net.ftp.FTPClientConfig;
    import ?org.apache.commons.net.ftp.FTPReply;
    import ?org.uranus.util.StringUtils;

    /**
    *?基于apache組織的commonNet開源組件實現ftp客戶端<br>
    *?本程序運行依賴于一個config成員變量屬性,其是一個屬性(Properties)對象<br>
    *?系統要求這個config對象,必需有如下屬性key:<br>
    *?server(ftp服務器ip地址或域名)<br>
    *?username(登錄用戶名)<br>
    *?password(登錄密碼)<br>
    *?如下屬性key是可選的:<br>
    *?systemKey(ftp服務器的操作系統key,如window)<br>
    *?serverLanguageCode(ftp服務器使用的語言,如zh)<br>
    *?<br>
    *?本程序是線程安全,在每一個上傳時都將創建ftp連接,上傳結束后再關閉ftp連接<br>
    *
    *?例子:<br>
    *?InputStream?localIn?=?new?FileInputStream("c:\\計算機.txt");<br>
    *
    *?java.util.Properties?config?=?new?Properties();<br>
    *?config.setProperty("server",?"192.168.5.120");<br>
    *?config.setProperty("username",?"upload");<br>
    *?config.setProperty("password",?"upload");<br>
    *
    *?FtpClientCommonNetImpl?client?=?new?FtpClientCommonNetImpl();<br>
    *?client.setConfig(config);<br>
    *?client.upload(localIn,?"/aaa/計算機.txt",?true);<br>
    *
    *
    *?
    @author ?zhangdb
    *
    */

    public ? class ?FtpClientCommonNetImpl? implements ?IFtpClient? {
    // ?ftp配置
    private ?Properties?config;

    /**
    *?連接到ftp服務器
    *
    *?
    @param ?server
    *?
    @param ?userName
    *?
    @param ?password
    *?
    @return
    *?
    @throws ?FtpException
    */

    protected ?FTPClient?connectFtpServer(String?server,?String?userName,
    String?password)?
    throws ?FtpException? {
    // ?創建ftp客戶端對象
    FTPClient?ftp? = ? new ?FTPClient();
    try ? {
    ftp.configure(
    this .getFTPClientConfig());
    // ?連接ftp服務器
    ftp.connect(server);
    // ?登錄
    ftp.login(userName,?password);

    // ?返回值
    int ?reply? = ?ftp.getReplyCode();
    if ?(( ! FTPReply.isPositiveCompletion(reply)))? {
    ftp.disconnect();
    throw ? new ?FtpException( " 登錄ftp服務器失敗,請檢查server[ " ? + ?server
    + ? " ]、username[ " ? + ?userName? + ? " ]、password[ " ? + ?password
    + ? " ]是否正確! " );
    }


    return ?ftp;
    }
    ? catch ?(Exception?ex)? {
    throw ? new ?FtpException(ex);
    }

    }


    /**
    *?關閉連接
    *
    *?
    @param ?ftp
    */

    protected ? void ?disconnectFtpServer(FTPClient?ftp)? {
    try ? {
    ftp.logout();
    ftp.disconnect();
    }
    ? catch ?(Exception?ex)? {
    throw ? new ?FtpException(ex);
    }

    }


    /**
    *?上傳
    */

    public ? void ?upload(InputStream?localIn,?String?remoteFilePath)
    throws ?FtpException? {
    String?server?
    = ? this .config.getProperty( " server " );
    String?userName?
    = ? this .config.getProperty( " username " );
    String?password?
    = ? this .config.getProperty( " password " );
    FTPClient?ftp?
    = ? this .connectFtpServer(server,?userName,?password);

    try ? {
    boolean ?result? = ?ftp.storeFile( this
    .enCodingRemoteFilePath(remoteFilePath),?localIn);
    if ?( ! result)? {
    throw ? new ?FtpException( " 文件上傳失敗! " );
    }

    }
    ? catch ?(Exception?ex)? {
    throw ? new ?FtpException(ex);
    }
    ? finally ? {
    this .disconnectFtpServer(ftp);
    }

    }


    /**
    *?上傳結束以后關閉輸入流
    *
    *?
    @param ?localIn
    *?
    @param ?remoteFilePath
    *?
    @param ?afterUploadCloseInputStream
    *?
    @throws ?FtpException
    */

    public ? void ?upload(InputStream?localIn,?String?remoteFilePath,
    boolean ?afterUploadCloseInputStream)? throws ?FtpException? {
    try ? {
    // ?上傳
    this .upload(localIn,?remoteFilePath);
    }
    ? finally ? {
    if ?(afterUploadCloseInputStream)? {
    if ?(localIn? != ? null )? {
    try ? {
    localIn.close();
    }
    ? catch ?(Exception?ex)? {
    throw ? new ?FtpException(ex);
    }

    }

    }

    }

    }


    /**
    *?得到配置
    *
    *?
    @return
    */

    protected ?FTPClientConfig?getFTPClientConfig()? {
    // ?創建配置對象
    FTPClientConfig?conf? = ? new ?FTPClientConfig( this .config.getProperty(
    " systemKey " ,?FTPClientConfig.SYST_NT));
    conf.setServerLanguageCode(
    this .config.getProperty(
    " serverLanguageCode " ,? " zh " ));
    return ?conf;
    }


    /**
    *?遠程文件路徑編碼(上傳到ftp上的文件路徑)
    *
    *?
    @param ?remoteFilePath
    *?
    @return
    */

    protected ?String?enCodingRemoteFilePath(String?remoteFilePath)? {
    return ?StringUtils.gbkToIso8859EnCoding(remoteFilePath);
    }


    public ?Properties?getConfig()? {
    return ?config;
    }


    public ? void ?setConfig(Properties?config)? {
    this .config? = ?config;
    }


    }
    ?
    posted on 2007-03-28 19:42 Ken.Lee 閱讀(1060) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 苍井空亚洲精品AA片在线播放 | 国产成人AV片无码免费| 1000部拍拍拍18免费网站| 四虎影视在线永久免费看黄| 亚洲Av无码精品色午夜 | 亚洲人成高清在线播放| 黄色免费网址大全| 999任你躁在线精品免费不卡| 色视频色露露永久免费观看| 亚洲av无码一区二区三区网站 | 精品国产免费人成网站| 毛片A级毛片免费播放| 亚洲中文字幕在线乱码| 亚洲欧美日韩一区二区三区| 国产无遮挡无码视频免费软件| 免费无码又爽又高潮视频| 亚洲av中文无码乱人伦在线r▽ | 一级午夜a毛片免费视频| 亚洲精品免费网站| 亚洲成AV人片一区二区| 九九精品国产亚洲AV日韩| 最近2019免费中文字幕视频三| 久久久青草青青国产亚洲免观 | 久久www免费人成看片| 国产精品亚洲二区在线观看| 亚洲av无码久久忘忧草| 久久99免费视频| 亚洲A∨午夜成人片精品网站| 亚洲伊人久久大香线蕉影院| a毛片视频免费观看影院| 免费中文字幕一级毛片| 亚洲一级大黄大色毛片| 日本一道本不卡免费| 男人的天堂亚洲一区二区三区| 亚洲综合在线视频| 亚欧洲精品在线视频免费观看| 免费无码不卡视频在线观看| 亚洲无砖砖区免费| 男的把j放进女人下面视频免费| 久久精品国产精品亚洲人人| MM1313亚洲国产精品|