关于使用java FTP是否可以实现断点下载?

sxlcom 2011-12-01 04:12:39
关于使用java FTP是否可以实现断点下载?
最好有实例,先谢了
...全文
151 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxlcom 2011-12-02
  • 打赏
  • 举报
回复
大家都没有遇到过类似的情况吗?
sxlcom 2011-12-01
  • 打赏
  • 举报
回复
网上找了一下,发现以下代码并没有在下载某个文件出现网络异常时,在该文件上己下载的基础上继续下载的功能,而是继续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;
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧