<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)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 日本免费电影一区| 99久久99这里只有免费费精品| 成年人在线免费观看| 亚洲视频2020| 精品女同一区二区三区免费站| 日本久久久久亚洲中字幕| 69pao强力打造免费高清| 亚洲精品福利网泷泽萝拉| 免费99精品国产自在现线| 亚洲宅男精品一区在线观看| 最新中文字幕电影免费观看| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | 特级淫片国产免费高清视频| 亚洲色偷偷综合亚洲av78| 免费理论片51人人看电影| 美女视频黄a视频全免费网站色| 亚洲国产小视频精品久久久三级 | 久久精品国产亚洲AV麻豆不卡| 两个人看的www免费| 亚洲韩国在线一卡二卡| 成全影视免费观看大全二| 精品国产亚洲AV麻豆| 久久影院亚洲一区| 无码精品一区二区三区免费视频| 亚洲日韩乱码中文无码蜜桃臀| 97无码免费人妻超级碰碰夜夜| 羞羞漫画登录页面免费| 亚洲精品蜜桃久久久久久| 日本免费一区二区在线观看| 亚洲成a人无码亚洲成www牛牛| 亚洲一级特黄大片在线观看| 91青青国产在线观看免费| 国产成人亚洲综合一区| 九月婷婷亚洲综合在线| 日韩午夜理论免费TV影院| 亚洲成a人无码亚洲成av无码| 国产亚洲AV无码AV男人的天堂| 福利免费观看午夜体检区| 成人网站免费大全日韩国产| 亚洲a级在线观看| 亚洲热线99精品视频|