0
点赞
收藏
分享

微信扫一扫

java实现ftp(common-net)

public boolean upload(String host, String userId, String passWord, String srcRoot, String fileName, String destRoot) throws Exception{ 

    

   FTPClient ftp = new FTPClient(); 

      int reply; 

      boolean success = false; 

      

      try {             

             
ftp.connect(host); 

             
ftp.login(userId,passWord); 

             reply = 
ftp.getReplyCode(); 

                

             if(FTPReply.isPositiveCompletion(reply)){ 

              logger.info("Connected to " + host); 

             } else { 

                 logger.info("Connection to " + host + " Failed"); 

                 
ftp.disconnect(); 

                 throw new Exception("FTP Connection Failed."); 

             } 

             // connected to the host 

             

             // delete the file, if it exists 

             
ftp.deleteFile(destRoot+fileName); 

             

             // upload the given file 

             FileInputStream input = new FileInputStream(srcRoot + fileName); 

             logger.info("Got a inputstream..");           

             success = 
ftp.storeUniqueFile(destRoot+fileName, input); 

             input.close(); 

         } catch (SocketException ex) { 

             logger.error(ex.getMessage()); 

             throw new Exception (ex.getMessage()); 

         } catch (IOException ex) { 

             logger.error(ex.getMessage()) ; 

             throw new Exception(ex.getMessage()); 

         } finally { 

          if ((ftp != null) && 
ftp.isConnected()) 

            
ftp.disconnect() ; 

         } 

         

         return success; 

     }

举报

相关推荐

0 条评论