关于使用java FTP是否可以实现断点下载?
最好有实例,先谢了
最好有实例,先谢了
解决方案 »
- java编程里如何生成Win32应用 程序(后缀名为.bat的)
- system.out.println("i= " i+ " j= "+j) 请教问题.谢谢!
- JSP页面刷新闪烁的问题
- paint事件的问题
- 请问哪里有JAVA学习的好中文网站
- 我下载了一个JCreator,但是不知道该如何配置,请使用过的朋友告诉我该如何使用、培植JCreator.谢谢!!
- 有关如何放置线程位置的问题
- OOP编程
- 紧急:请问谁知道如何在JAVA中去调用执行操作系统中的其他可执行文件啊?
- 请问我接受到的这种编码是什么,如果转化成汉字 (%C4%FA%B5%C4%CA%D6%BB%FA%BA%C5%C2%EB%D4%CB)
- java访问服务器 网络故障解决的办法
- 一个很伤脑的笔试题
不知大家是否有好的方法实现这样的功能?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;
}