葉落知秋

          一葉落而知天下秋

          統(tǒng)計(jì)

          留言簿(1)

          Java相關(guān)

          閱讀排行榜

          評(píng)論排行榜

          ftp實(shí)現(xiàn)上傳


          發(fā)表時(shí)間: 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服務(wù)器  
          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文件到服務(wù)器  
          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.     * 遠(yuǎn)程文件路徑編碼(上傳到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 飛雪連天 閱讀(154) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 鞍山市| 阿合奇县| 孝义市| 云浮市| 寻乌县| 金门县| 永康市| 长武县| 哈尔滨市| 咸宁市| 灵寿县| 和顺县| 遂昌县| 沐川县| 凯里市| 张家界市| 拜泉县| 巍山| 梁河县| 玉溪市| 武冈市| 浑源县| 喀什市| 仪陇县| 泗洪县| 清涧县| 靖安县| 三明市| 延川县| 平潭县| 江口县| 鄂托克旗| 新丰县| 马公市| 普陀区| 上林县| 宜兰市| 嵊泗县| 岱山县| 临武县| 大邑县|