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

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

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

    葉落知秋

    一葉落而知天下秋

    統計

    留言簿(1)

    Java相關

    閱讀排行榜

    評論排行榜

    ftp實現上傳


    發表時間: 2007年11月07日 



    采用 apache的 commons-net包

    java 代碼
    1. import java.io.FileInputStream;   
    2. import java.io.IOException;   
    3.   
    4. import org.apache.commons.net.ftp.FTP;   
    5. import org.apache.commons.net.ftp.FTPClient;   
    6. import org.apache.commons.net.ftp.FTPReply;   
    7. import org.apache.log4j.Logger;   
    8.   
    9. public class FtpUtil {   
    10.     private Logger log = Logger.getLogger(this.getClass());   
    11.     private String ftpServer;   
    12.     private String userName;   
    13.     private String pswd;   
    14.        
    15.     public void setFtpServer(String ftpServer) {   
    16.         this.ftpServer = ftpServer;   
    17.     }   
    18.   
    19.     public void setPswd(String pswd) {   
    20.         this.pswd = pswd;   
    21.     }   
    22.   
    23.     public void setUserName(String userName) {   
    24.         this.userName = userName;   
    25.     }   
    26.        
    27.     /**  
    28.      * 連接Ftp服務器  
    29.      * @return  
    30.      * @throws SmpBizException  
    31.      */  
    32.     public  FTPClient loginToFtpServer() throws Exception {   
    33.            
    34.         FTPClient ftp = new FTPClient();   
    35.         try {   
    36.             ftp.connect(this.ftpServer);   
    37.             int reply = ftp.getReplyCode();   
    38.             if(!FTPReply.isPositiveCompletion(reply)){   
    39.                 log.error("Ftp connect to "+this.ftpServer +" failed!");   
    40.                 throw new Exception("Ftplogin to connect to "+this.ftpServer +" failed!");   
    41.             }   
    42.             ftp.login(this.userName,this.pswd);   
    43.             log.debug("Ftp connect to "+this.ftpServer +" success!");   
    44.   
    45.         } catch (Exception e) {   
    46.             log.error("Ftp connect "+this.ftpServer +" failed!!");   
    47.             throw new Exception("FtpLogin to "+this.ftpServer +" failed!!");   
    48.         }   
    49.         return ftp;   
    50.     }   
    51.        
    52.     /**  
    53.      * Ftp文件到服務器  
    54.      * @param remotPath  
    55.      * @param localFile  
    56.      * @param remoteFileName  
    57.      * @throws SmpBizException  
    58.      */  
    59.     public void upload(String remotPath,String localFile,String remoteFileName) throws Exception{   
    60.         FTPClient ftp = this.loginToFtpServer();   
    61.         FileInputStream in = null;   
    62.         try {   
    63.             log.debug("--- ftp begin! ---");   
    64.             ftp.setFileType(FTP.BINARY_FILE_TYPE);    
    65.             ftp.enterLocalPassiveMode();                
    66.             if(null!=remotPath&&(!ftp.changeWorkingDirectory(remotPath))){   
    67.                 ftp.makeDirectory( remotPath );    
    68.                 ftp.changeWorkingDirectory( remotPath );                    
    69.             }   
    70.   
    71.             in = new FileInputStream(localFile);   
    72.             ftp.storeFile(remoteFileName, in);   
    73.             log.debug("--- ftp finish! ---");   
    74.         } catch (IOException e) {   
    75.             log.error("ftp file:"+ localFile +" fail!!!");   
    76.             throw new Exception("ftp file:"+ localFile +" fail!!!");   
    77.         }finally{   
    78.             try {   
    79.                 ftp.disconnect();   
    80.                 if(null!=in){   
    81.                     in.close();   
    82.                 }   
    83.             } catch (IOException e) {   
    84.                 this.log.error("close connect failed!");   
    85.                 e.printStackTrace();   
    86.             }   
    87.         }   
    88.     }   
    89.        
    90.     /**   
    91.     * 遠程文件路徑編碼(上傳到ftp上的文件路徑)   
    92.     *   
    93.     * @param remoteFilePath   
    94.     * @return   
    95.     */    
    96.     protected String enCodingRemoteFilePath(String remoteFilePath) {    
    97.           return null;    
    98.     }       
    99.           
    100. }  

    posted on 2007-11-07 17:24 飛雪連天 閱讀(151) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 18级成人毛片免费观看| 日韩电影免费在线观看中文字幕| 中文字幕无码播放免费| 在线电影你懂的亚洲| 国产自国产自愉自愉免费24区 | 国内精品久久久久影院免费| 久久精品亚洲男人的天堂| 一级毛片a免费播放王色| 亚洲国产精品第一区二区三区| 特级毛片A级毛片免费播放| 亚洲国产精品成人一区| 国产免费A∨在线播放| 亚洲午夜未满十八勿入网站2| 成人无码视频97免费| 亚洲精品无码av人在线观看| 日韩电影免费在线观看网站| 麻豆亚洲AV永久无码精品久久| 999久久久免费精品国产| 亚洲国产成人综合精品| 免费一级e一片在线播放| 中文永久免费观看网站| 老色鬼久久亚洲AV综合| 无码人妻精品一二三区免费| 国产午夜亚洲精品不卡免下载| 亚洲无码在线播放| 99久久精品免费精品国产| 亚洲 欧洲 自拍 另类 校园| 四虎影视在线永久免费看黄 | 亚洲成在人线电影天堂色| 日韩成人免费在线| 两个人看的www视频免费完整版| 久久亚洲国产精品成人AV秋霞| 成年男女男精品免费视频网站 | 亚洲成人网在线观看| 日韩激情淫片免费看| 久久免费香蕉视频| 亚洲AV无码乱码麻豆精品国产| 一本色道久久88亚洲综合| 无码中文字幕av免费放dvd| 亚洲色偷偷偷综合网| 亚洲精品成人片在线播放|