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

📄 controler.java

📁 使用Eclipse编写的java的网络图片爬虫
💻 JAVA
字号:
package gather;

import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedList;

public class controler
{
	public static LinkedList	URLList			= new LinkedList();
	public static LinkedList	IMGList			= new LinkedList();
	public static LinkedList	MissionList		= new LinkedList();

	public static int			nThreadCount	= 0;
	public final static int		nMaxThread		= 50;
	public final static int		nMaxURLLink		= 10000;//暂不使用

	
	public controler()
	{

	}

	public static void start()
	{
		String strlinktext = "";
		boolean bsavetofile = false;
		int nAutoStopSignal=0;
		while (true)
		{
			try
			{
				System.out.println("---->当前的线程总数:" + nThreadCount + "  ;待处理的任务数:" + controler.MissionList.size() + "[ 线程数量限制:" + nMaxThread + "]");
				//-------------------------------------------------------------------------
				if ((nThreadCount == 0) && (controler.MissionList.size() == 0))
				{
					nAutoStopSignal++;
					if(nAutoStopSignal>=60)
					{
						System.out.println("无线程、无任务的状态已经等待60秒,将自动停止...");
						break;
					}
				}
				if ((nThreadCount < nMaxThread))
				{//控制线程的数量:必须少于nMaxThread:
					strlinktext = (String) controler.MissionList.pollFirst();
					if (null == strlinktext || strlinktext.length() <= 0)
					{
						System.out.println("待处理的任务列表是空的。");
						Thread.sleep(1000);
						continue;
					}
					strlinktext = strlinktext.toLowerCase();
					System.out.println("------------------------>"+strlinktext);
					if (strlinktext.endsWith(".css") || strlinktext.equals("../"))
						continue;
					if (strlinktext.endsWith(".jpg") || strlinktext.endsWith(".bmp") || strlinktext.endsWith(".jpeg"))
					{//需要保存:到文件的链接:
						bsavetofile = true;
						//该链接是新的,因为列表中没有:
						//-----------------
						//测试:将链接记录到文件:
						new File("D:\\Temp\\").mkdirs();
						FileOutputStream fos = new FileOutputStream("D:\\Temp\\savetofile.txt", true);
						String strfile = strlinktext + "\r\n";
						fos.write(strfile.getBytes());
						fos.close();
					}
					else
					{//需要解析的:新的、待解析的、网页链接:
						bsavetofile = false;
						//该链接是新的,因为列表中没有:
						controler.URLList.add(strlinktext);
						//-----------------
						//测试:将链接记录到文件:
						new File("D:\\Temp\\").mkdirs();
						FileOutputStream fos = new FileOutputStream("D:\\Temp\\已处理的URL.txt", true);
						String strfile = strlinktext + "\r\n";
						fos.write(strfile.getBytes());
						fos.close();
					}
					//--------------------------------------------------------------
					if (strlinktext.length() > 0)
					{//创建线程并开始运行:
						Thread g = new gather(strlinktext, bsavetofile);
						g.start();//
					}
				}
				else
				{//如果线程数量到达门限,则休息1秒:
					Thread.sleep(1000);
				}
			}
			catch (Exception e)
			{
				//				System.out.println("ERROR: controler_start_"+e);
			}
		}
		System.out.println("全部任务处理完毕![线程数:" + nThreadCount + "  ;待处理的任务数:" + controler.MissionList.size() + "]");
	}

	public static void main(String[] args)
	{
		//运行入口:
		//在此输入种子网址:
		String str = "";
		try
		{
			str="http://desktop.xaonline.com/list.asp?category=桌面壁纸&subcategory=风光";
//			str="http://www.chinatuku.com/fg/dongxuemeijing/index.htm";
//			str="http://www.8825.com/6_Class.asp";
//			str = "http://www.ivsky.com/";
//			str = "http://www.bzkoo.com/";
//			str = "http://www.mydeskcity.com/";
//			str = "http://www.tuhai.org/Untitled-1.html";
//			str = "http://www.ivsky.com/Photo/835/87278.html";

			controler.MissionList.add(str);
			controler.start();
		}
		catch (Exception e)
		{
			System.out.println("当前 nRunCount:" + gather.nRunCount);
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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