我给你一个我写的ftp下载的(分三个帖子),http我有,你来mail吧,我的mail:[email protected]_info.java//得到url等
import java.net.*;
import java.io.*; class File_info
{
String sDown_file; public File_info(String s1)
{
sDown_file = s1;
} public String get_url()
{
int nStart_number = sDown_file.indexOf("//");
if (nStart_number == -1)
{
return "-1" ;//-1表明此地址不是ftp地址,不含有"//"
}
else
{
int nSecond_number = sDown_file.indexOf("/",nStart_number+3);
if (nSecond_number == -1)
{
return "-1" ; //-1表明此地址不是ftp地址,不含有"/"
}
else
{
String url = sDown_file.substring(nStart_number + 2,nSecond_number);
return url;
}
}
} public String get_down_file()
{
int nStr_length = sDown_file.length();
int nStart_number = sDown_file.indexOf("//");
if (nStart_number == -1)
{
return "-1" ;//-1表明此地址不是ftp地址,不含有"//"
}
else
{
int nSecond_number = sDown_file.indexOf("/",nStart_number+3);
if (nSecond_number == -1)
{
return "-1"; //-1表明此地址不是ftp地址,不含有"/"
}
else
{
String url = sDown_file.substring(nSecond_number + 1 , nStr_length);
return url;
}
}
} public String get_IP()
{
String url = this.get_url();
try
{
String ip = InetAddress.getByName(url).toString();
return ip;
}
catch(UnknownHostException e)
{
return "-1"; //-1表示错误的主机
}
catch(IOException e2)
{
return "-2";//-2表示错误的输入输出流
}
} } FileAccess.java//写入文件 import java.io.*; public class FileAccess implements Serializable
{
RandomAccessFile oSaveFile = null ;
long nPos; public FileAccess(String sName,long npos) throws IOException
{
oSaveFile = new RandomAccessFile(sName,"rw");
this.nPos = npos;
oSaveFile.seek(nPos);
} public synchronized int write(byte[] b, int nStart, int nLen)
{
try
{
int n = -1 ;
oSaveFile.write(b,nStart,nLen);
n = nLen;
return n;
}
catch(IOException e)
{
return -2;
}
}
import java.net.*;
import java.io.*; class File_info
{
String sDown_file; public File_info(String s1)
{
sDown_file = s1;
} public String get_url()
{
int nStart_number = sDown_file.indexOf("//");
if (nStart_number == -1)
{
return "-1" ;//-1表明此地址不是ftp地址,不含有"//"
}
else
{
int nSecond_number = sDown_file.indexOf("/",nStart_number+3);
if (nSecond_number == -1)
{
return "-1" ; //-1表明此地址不是ftp地址,不含有"/"
}
else
{
String url = sDown_file.substring(nStart_number + 2,nSecond_number);
return url;
}
}
} public String get_down_file()
{
int nStr_length = sDown_file.length();
int nStart_number = sDown_file.indexOf("//");
if (nStart_number == -1)
{
return "-1" ;//-1表明此地址不是ftp地址,不含有"//"
}
else
{
int nSecond_number = sDown_file.indexOf("/",nStart_number+3);
if (nSecond_number == -1)
{
return "-1"; //-1表明此地址不是ftp地址,不含有"/"
}
else
{
String url = sDown_file.substring(nSecond_number + 1 , nStr_length);
return url;
}
}
} public String get_IP()
{
String url = this.get_url();
try
{
String ip = InetAddress.getByName(url).toString();
return ip;
}
catch(UnknownHostException e)
{
return "-1"; //-1表示错误的主机
}
catch(IOException e2)
{
return "-2";//-2表示错误的输入输出流
}
} } FileAccess.java//写入文件 import java.io.*; public class FileAccess implements Serializable
{
RandomAccessFile oSaveFile = null ;
long nPos; public FileAccess(String sName,long npos) throws IOException
{
oSaveFile = new RandomAccessFile(sName,"rw");
this.nPos = npos;
oSaveFile.seek(nPos);
} public synchronized int write(byte[] b, int nStart, int nLen)
{
try
{
int n = -1 ;
oSaveFile.write(b,nStart,nLen);
n = nLen;
return n;
}
catch(IOException e)
{
return -2;
}
}
import java.io.*; class Ftp_down extends Thread
{
String sDown_file_url; //下载的文件路径
String sDown_file;//下载的文件
String sSave_file; //保存的文件
int iThread_number; //线程数
long[] nStartPos;
long[] nEndPos;
long nFilelength;
boolean bFirst = true;
File Tmpfile;
File_info file_info = null;
Socket check_reget_socket = null;
String sFile_url;
String sFile_ip;
String Down_file;
String sUser;
String sPwd;
Thread_ftp[] ftp;
boolean Is_start = false ;//检测是否可以开始下载 public Ftp_down(String s1,String s2,String s3,String s4,int i1)
{
sDown_file_url = s1;
sSave_file = s2 ;
iThread_number = i1;
nStartPos = new long[iThread_number];
nEndPos = new long[iThread_number];
file_info = new File_info(sDown_file_url);
this.sUser = s3;
this.sPwd = s4;
Thread_ftp[] ftp = null;
} public void run()
{
sFile_url = file_info.get_url();
System.out.println(this.sFile_url);
sDown_file = file_info.get_down_file();
BufferedReader check_in;
PrintWriter check_out;
if (sFile_url != "-1")
{
try
{
this.check_reget_socket = new Socket(this.sFile_url,21);
this.check_reget_socket.setSoTimeout(4000);
System.out.println(this.sFile_url);
check_in = new BufferedReader(new InputStreamReader(check_reget_socket.getInputStream()));
check_out = new PrintWriter(check_reget_socket.getOutputStream(),true);
if (this.getResponse(check_in,220))//连接主机成功与否
{
this.Is_start = true ;
check_out.println("USER " + this.sUser);
}
else
{
this.Is_start = false ;
}
if(this.getResponse(check_in,331) && this.Is_start)//检测用户名
{
this.Is_start = true;
check_out.println("PASS " + this.sPwd);
}
else
{
this.Is_start = false ;
System.out.println("用户未通过");
}
if (this.getResponse(check_in,230) && this.Is_start)//检测密码
{
this.Is_start = true ;
check_out.println("REST 100");
}
else
{
this.Is_start = false ;
}
if (this.getResponse(check_in,350) && this.Is_start)//检测是否支持断点续传
{
this.Is_start = true;
System.out.println("该站点支持断点续传");
check_out.println("SIZE /"+this.sDown_file);
String sFileLength = check_in.readLine();
System.out.println(sFileLength);
if(sFileLength.substring(0,3).equals("213"))
{
check_in.close();
check_out.close();
this.check_reget_socket.close();
this.nFilelength = Long.parseLong(sFileLength.substring(4,sFileLength.length()));
System.out.println(this.nFilelength);
for(int i = 0;i < this.iThread_number;i++)
{
this.nStartPos[i] = (long)(i*(this.nFilelength/this.iThread_number));
}
for(int i = 0;i < this.iThread_number - 1;i++)
{
this.nEndPos[i] = this.nStartPos[i+1] ;
}
this.nEndPos[this.nEndPos.length - 1] = this.nFilelength;
ftp = new Thread_ftp[this.iThread_number];
for(int i = 0;i < this.iThread_number;i++)
{
ftp[i] = new Thread_ftp(this.nStartPos[i],this.nEndPos[i],this.sFile_url,this.sUser,this.sPwd,this.sSave_file,this.sDown_file);
ftp[i].start();
System.out.println("线程"+i+"在Ftp_down启动");
}
}
}
else
{
System.out.println("该站点不支持断点续传");
}
}
catch(UnknownHostException e)
{
System.out.println("未知的错误1:未知的主机"+e.toString());
}
catch(IOException e1)
{
System.out.println("未知的错误2:输入输出错误"+e1.toString());
}
}
} private boolean getResponse(BufferedReader bufferredreader,int RightResponseCode)
{
String s ;
boolean more = true ;
boolean Right = false ;
try
{
while(more)
{
s = bufferredreader.readLine();
if(s.length() == 0)
{
s = bufferredreader.readLine();
}
if (String.valueOf(s.charAt(3)).equals("-"))
{
System.out.println(s);
}
else
{
more = false ;
int number = Integer.parseInt(s.substring(0,3));
System.out.println(number);
if (number == RightResponseCode)
{
Right = true;
}
else
{
Right = false;
}
}
}
}
catch (IOException w)
{
Right = false;
}
return Right;
}
} Thread_ftp.java//子线程 import java.net.*;
import java.io.*; public class Thread_ftp extends Thread
{
long nStartPos;
long nEndPos;
String sFile_url;
final int PORT = 21;
Socket Get_data_socket = null;
Socket Get_info_socket = null;
DataInputStream get_data = null;
boolean is_Down = false ;
boolean is_Stop = false ;
String sUser;
String sPwd;
String sInfo;
String sSave_file;
String sDown_file;
FileAccess fileaccess = null; public Thread_ftp(long number_1,long number_2,String s1,String s2,String s3,String s4,String s5) throws IOException
{
nStartPos = number_1;
nEndPos = number_2;
sFile_url = s1;
sUser = s2;
sPwd = s3;
sSave_file = s4;
sDown_file = s5;
fileaccess = new FileAccess(this.sSave_file,this.nStartPos);
} public void run()
{
while(this.nStartPos < this.nEndPos && !is_Stop)
{
try
{
this.Get_info_socket = new Socket(this.sFile_url,this.PORT);
PrintWriter put_info = new PrintWriter(this.Get_info_socket.getOutputStream(),true);
BufferedReader get_info = new BufferedReader(new InputStreamReader(this.Get_info_socket.getInputStream()));
put_info.println("USER " + this.sUser);
this.getInfo(get_info);
put_info.println("PASS " + this.sPwd);
this.getInfo(get_info);
put_info.println("TYPE I");
this.getInfo(get_info);
put_info.println("PASV");
this.getInfo(get_info);
sInfo = get_info.readLine();
System.out.println(sInfo + "OK");
put_info.println("REST " + this.nStartPos);
this.getInfo(get_info);
this.Get_data_socket = new Socket(this.get_info_ip(this.sInfo,0),Integer.parseInt(this.get_info_ip(this.sInfo,1)));
put_info.println("RETR /" + this.sDown_file);
this.getInfo(get_info);
this.Get_info_socket.close();
this.get_data = new DataInputStream(this.Get_data_socket.getInputStream()); byte[] b = new byte[1024];
int nRead;
while((nRead = this.get_data.read(b,0,1024)) > 0 && this.nStartPos < this.nEndPos && !is_Stop)
{
this.nStartPos = this.fileaccess.write(b,0,nRead);
}
this.is_Down = true ;
this.is_Stop = true ;
System.out.println("文件传输完毕,谢谢"); }
catch(UnknownHostException e)
{
this.is_Stop = true;
System.out.println("意外的错误3:未知的主机"+e.toString());
}
catch(IOException e2)
{
this.is_Stop = true;
System.out.println("意外的错误4:输入输出错误"+e2.toString());
}
}
} public boolean getInfo(BufferedReader buffer)
{
boolean more = true;
boolean is_OK = false;
String s; try{
while(more)
{
s = buffer.readLine();
if (s.length() == 0)
{
s = buffer.readLine();
}
System.out.println(s);
if (String.valueOf(s.charAt(3)).equals(" "))
{
more = false ;
is_OK = true;
}
}
}
catch (IOException e)
{
is_OK = false;
}
return is_OK;
} public String get_info_ip(String string,int i)
{
int number_1;
int number_2;
int number_3;
int number_4;
int number_5;
int number_6;
int number_7;
int port_1;
int port_2;
String port; number_1 = string.indexOf("(");
number_2 = string.indexOf(",");
number_3 = string.indexOf(",",number_2+1);
number_4 = string.indexOf(",",number_3+1);
number_5 = string.indexOf(",",number_4+1);
number_6 = string.indexOf(",",number_5+1);
number_7 = string.indexOf(")",number_6+1);
String first_ip = string.substring(number_1+1,number_2);
String second_ip = string.substring(number_2+1,number_3);
String third_ip = string.substring(number_3+1,number_4);
String forth_ip = string.substring(number_4+1,number_5);
port_1 = Integer.parseInt(string.substring(number_5+1,number_6));
port_2 = Integer.parseInt(string.substring(number_6+1,number_7));
port = String.valueOf((port_1 << 8) + port_2); if (i == 0)
{
return first_ip + "." + second_ip + "." + third_ip + "." + forth_ip;
}
else
{
if (i == 1)
{
return port;
}
else
{
return "";
}
} }
}
class test
{
public static void main(String[] args)
{
Ftp_down ftp_down = new Ftp_down("ftp://192.168.0.100/vs_demo.exe","c:\\vs_demo.exe","administrator","******",5);
ftp_down.start();
}
}//ftp://192.168.0.100/vs_demo.exe//要下载的文件
c://vs_demo.exe//保存的路径
adminisrator//用户名
******//密码,这是我公司的三,不好说出来,哈哈
[email protected]