关于使用java  FTP是否可以实现断点下载?
最好有实例,先谢了

解决方案 »

  1.   

    网上找了一下,发现以下代码并没有在下载某个文件出现网络异常时,在该文件上己下载的基础上继续下载的功能,而是继续Login
    不知大家是否有好的方法实现这样的功能?public class TestProtocolFtp {       private FTPClient FTP ; 
        private String host ; 
        private int port ; 
        private String user ; 
        private String pwd ; 
        private String taskdate ; 
        private boolean forceexit ;       public String toString() { 
           return host + ":" + port + "@" + user + "/" + pwd ;     }       public boolean Login(String strHost, int nPort, String strUser, String strPwd) {        host = strHost; 
           port = nPort; 
           user = strUser; 
           pwd = strPwd; 
             boolean bOK = false ;        try { 
               if ( FTP == null ) { 
                  FTP = new FTPClient(); 
               } else { 
                  try { 
                      FTP .disconnect(); 
                  } catch (Exception e) {               }            } 
               FTP .setDefaultTimeout(3600 * 1000); 
               //FTP .setConnectTimeout(3600 * 1000); 
               FTP .connect(strHost, nPort);              int reply = FTP .getReplyCode(); 
               if (!FTPReply.isPositiveCompletion (reply)) { 
                  FTP .disconnect(); 
                  System.out.println("FTP server refused connection." ); 
                  return false ;            }              bOK = FTP .login(strUser, strPwd); 
               if (bOK) { 
                  FTP.enterLocalPassiveMode(); 
                  FTP.setFileType(FTPClient. BINARY_FILE_TYPE ); // 设置为二进制传输模式 
                  FTP.setDataTimeout(3600 * 1000);            } else {              System.out.println("FTP server Login Failure Code:"+ FTP .getReplyCode());            }        } catch (SocketException se) {         System.out.println( "FTP login" + se);        } catch (Exception e) {         System.out.println( "FTP login" + e);        } 
           return bOK; 
        }     public void Close() {        try { 
               if ( FTP != null ) { 
                  FTP .logout(); 
                  FTP .disconnect(); 
               } 
           } catch (Exception e) { 
           } 
        }       private boolean ftpValidate() {        if (( FTP != null ) && ( FTP .isConnected())) 
               return true ; 
           else 
               return false ; 
        }       public boolean ReLogin() { 
           int i = 1; 
           boolean ret = false ;          if (ftpValidate()) { 
               return true ; 
           }        while (!ftpValidate()) { 
               if (i > 3) { 
                  return ret; 
               } 
              System.out.println(": 第 " + i + " 次 ReLogin 登陆 ftp:" + host + "," + user ); 
               try { 
                  Thread.sleep (1000 * i * 30);            } catch (Exception e) { 
                System.out.println( ": ftp relogin failed. " + e); 
               } 
               ret = Login( host , port , user , pwd ); 
               if (ret) 
                  break ;             i++; 
           } 
           return ret; 
        }       class MonitorThread extends Thread { 
           FTPClient ftpClient = null ; 
           int nSeconds = 0; 
           Thread ftpThread = null ;          public MonitorThread( int nSeconds, FTPClient ftp, Thread t) { 
               ftpClient = ftp; 
               this.nSeconds = nSeconds; 
               this.ftpThread = t; 
           }          public void run() { 
               if ( nSeconds > 0) { 
                  try { 
                   System.out.println(": sleep 开始 " ); 
                      Thread.sleep ((( long ) nSeconds ) * 1000); 
                      System.out.println( ": sleep 结束 " );               } catch (InterruptedException e) { 
                      System.out.println( ": Monitor thread interrupted by ftp thread" ); 
                      return ; 
                  }               try { 
                   System.out.println(" ftp timeout for " + nSeconds + " seconds, interrupt ftp thread" );                   ftpThread .interrupt();               } catch (Exception e) {                System.out.println( "Interrupt ftp error:" + e);               }            }        }     } 
        private long contentLength = 0; 
    }