⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp_down.java

📁 支持多线程的ftp下载源程序,没有用任何第三方组件
💻 JAVA
字号:
package ext.down;

import java.net.*;
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;// 检测是否可以开始下载

	private boolean VERBOSE = 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();
		if (VERBOSE)
			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);
				if (VERBOSE)
					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();
					if (VERBOSE)
						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()));
						if (VERBOSE)
							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, i);

							ftp[i].start();
							System.out.println("线程" + (i + 1) + "启动...");
						}
					}
				}
				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("-"))
				{
					if (VERBOSE)
						System.out.println(s);
				}
				else
				{
					more = false;
					int number = Integer.parseInt(s.substring(0, 3));
					if (VERBOSE)
						System.out.println(s + "===>" + number);
					if (number == RightResponseCode)
					{
						Right = true;
					}
					else
					{
						Right = false;
					}
				}
			}
		}
		catch (IOException w)
		{
			Right = false;
		}
		return Right;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -