<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開源組件實現(xiàn)ftp客戶端<br>
    *?本程序運(yùn)行依賴于一個config成員變量屬性,其是一個屬性(Properties)對象<br>
    *?系統(tǒng)要求這個config對象,必需有如下屬性key:<br>
    *?server(ftp服務(wù)器ip地址或域名)<br>
    *?username(登錄用戶名)<br>
    *?password(登錄密碼)<br>
    *?如下屬性key是可選的:<br>
    *?systemKey(ftp服務(wù)器的操作系統(tǒng)key,如window)<br>
    *?serverLanguageCode(ftp服務(wù)器使用的語言,如zh)<br>
    *?<br>
    *?本程序是線程安全,在每一個上傳時都將創(chuàng)建ftp連接,上傳結(jié)束后再關(guān)閉ftp連接<br>
    *
    *?例子:<br>
    *?InputStream?localIn?=?new?FileInputStream("c:\\計算機(jī).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/計算機(jī).txt",?true);<br>
    *
    *
    *?
    @author ?zhangdb
    *
    */

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

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

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

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


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

    }


    /**
    *?關(guān)閉連接
    *
    *?
    @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);
    }

    }


    /**
    *?上傳結(jié)束以后關(guān)閉輸入流
    *
    *?
    @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()? {
    // ?創(chuàng)建配置對象
    FTPClientConfig?conf? = ? new ?FTPClientConfig( this .config.getProperty(
    " systemKey " ,?FTPClientConfig.SYST_NT));
    conf.setServerLanguageCode(
    this .config.getProperty(
    " serverLanguageCode " ,? " zh " ));
    return ?conf;
    }


    /**
    *?遠(yuǎn)程文件路徑編碼(上傳到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 閱讀(1064) 評論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产午夜免费秋霞影院| 亚洲成人激情小说| 国产美女无遮挡免费视频网站| 亚洲综合国产一区二区三区| 三年片免费高清版 | 国产精品亚洲片在线| 天堂在线免费观看中文版| 亚洲hairy多毛pics大全| 亚洲日本中文字幕| 免费特级黄毛片在线成人观看| 日韩免费在线观看视频| 亚洲妇女熟BBW| 亚洲视频国产精品| 亚洲欧洲成人精品香蕉网| 免费人成视频在线观看不卡| 拍拍拍又黄又爽无挡视频免费| 一级做a爰片久久毛片免费陪 | 午夜电影免费观看| 日本免费网址大全在线观看| 91视频免费网站| 99久久国产亚洲综合精品| 亚洲黄色片在线观看| 久久久影院亚洲精品| 国产亚洲成av人片在线观看| 狠狠亚洲狠狠欧洲2019| 亚洲福利中文字幕在线网址| 国产91免费在线观看| 91短视频在线免费观看| 久久大香香蕉国产免费网站| 无码av免费网站| 日韩午夜理论免费TV影院 | 国产精品冒白浆免费视频 | 叮咚影视在线观看免费完整版| 97久久国产亚洲精品超碰热| 亚洲色大成网站www永久一区| 国产成人啪精品视频免费网| 免费无码黄网站在线观看| 在线观看免费成人| 午夜毛片不卡高清免费| 亚洲欧洲精品视频在线观看| 四虎精品亚洲一区二区三区|