`
zaife
  • 浏览: 40482 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ftp实现上传

    博客分类:
  • java
阅读更多

采用 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. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics