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

📄 main.java

📁 用来下载网上图片
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			
			String filterUrl="|";
			if(this.shtmlCheckBox.isSelected()){
				filterUrl+="shtml|";
			}
			if(this.htmlCheckBox.isSelected()){
				filterUrl+="html|";
			}
			if(this.aspxCheckBox.isSelected()){
				filterUrl+="aspx|";
			}
			if(this.aspCheckBox.isSelected()){
				filterUrl+="asp|";
			}
			if(this.htmCheckBox.isSelected()){
				filterUrl+="htm|";
			}
			if(this.jspCheckBox.isSelected()){
				filterUrl+="jsp|";
			}
			if(this.phpCheckBox.isSelected()){
				filterUrl+="php|";
			}
			if(this.doCheckBox.isSelected()){
				filterUrl+="do|";
			}
			
			if(filterUrl.equals("|")){
				JOptionPane.showMessageDialog(this, "您没有选择搜索的页面类型,将按默认类型来搜索");
			}else{
				filterUrl=filterUrl.substring(1, filterUrl.length()-1);
				WebInfo.FilterUrl=filterUrl;
				
			}
			filterUrl=null;
			
			
			String filterImage="|";
			if(this.jpgCheckBox.isSelected()){
				filterImage+="jpg|";
			}
			if(this.bmpCheckBox.isSelected()){
				filterImage+="bmp|";
			}
			if(this.pngCheckBox.isSelected()){
				filterImage+="png|";
			}
			if(this.gifCheckBox.isSelected()){
				filterImage+="gif|";
			}
			if(filterImage.equals("|")){			
				JOptionPane.showMessageDialog(this, "您没有选择搜索的图片类型,将按默认类型来搜索");
			}else{
				filterImage=filterImage.substring(1, filterImage.length()-1);
				WebInfo.FilterImage=filterImage;
				
			}
			
			
			filterImage=null;
			
			
		
			
			//启动下载线程
		
			con = new Connect();
            con.setPriority(6);
            con.start();
            

            
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	
	
	private void setDownLoadSetEnable(boolean f){      //设置下载按钮是否可用

		boolean flag=f;
		webURL.setEnabled(flag);
		savePath.setEnabled(flag);
		startPage.setEnabled(flag);
		fileSize.setEnabled(flag);
		maxThread.setEnabled(flag);
		searchDepth.setEnabled(flag);
		prefix.setEnabled(flag);
		
		shtmlCheckBox.setEnabled(flag);
		htmlCheckBox.setEnabled(flag);
		aspxCheckBox.setEnabled(flag);
		aspCheckBox.setEnabled(flag);
		htmCheckBox.setEnabled(flag);
		phpCheckBox.setEnabled(flag);
		doCheckBox.setEnabled(flag);
		
		jpgCheckBox.setEnabled(flag);
		pngCheckBox.setEnabled(flag);
		bmpCheckBox.setEnabled(flag);
		gifCheckBox.setEnabled(flag);
		
	}
	
	
	//检查设置是否正确
	
	private boolean CheckValid() {

		if (!Check.isWeb(this.webURL.getText())) {
			JOptionPane.showMessageDialog(this, "网站设置不正确");
			return false;
		}
		if (!Check.isPage(this.startPage.getText())) {
			JOptionPane.showMessageDialog(this, "起始页设置不正确");
			return false;
		}
		if (!Check.isEmpty(this.savePath.getText())) {
			JOptionPane.showMessageDialog(this, "保存路径不正确");
			return false;
		}
		
		
		
		
		if (!Check.isDigit(this.fileSize.getText())) {
			JOptionPane.showMessageDialog(this, "图片大小设置不正确");
			return false;
		}
		if (!Check.isWord(this.prefix.getText())) {
			JOptionPane.showMessageDialog(this, "前缀有非法字符");
			return false;
		}
		if (!Check.isDigit(this.maxThread.getText())) {
			JOptionPane.showMessageDialog(this, "最大连接数设置不正确");
			return false;
		}
		if (!Check.isDigit(this.searchDepth.getText())) {
			JOptionPane.showMessageDialog(this, "搜索深度设置不正确");
			return false;
		}
		return true;
	}

	public  void append(String s) {
		this.searchInfo.append(s + "\n");
		// con.flag=true;
	} 
  public boolean getStarted(){
	  return hasStart;
  } 
  
  
    ////////////////////////////////////////////////////////////////////////////////////////////////
  
    //内部下载线程定义
  
	class Connect extends Thread {

	final String encode = "gbk";

		private URL url;
		private URL web;
        private String startPage;
		private HashMap<String, Boolean> savedURL = new HashMap<String, Boolean>();

		private boolean flag;

		String prefix = "";

		Vector<GetImg> getimg;

		int MaxThread = 10;

		int MaxDapth = 2;

		int ActiveThreadCount = 0;

		private String website = "";

		private String CurrentPage;

		private String regex_html = "\\s+href=[\"']?((\\.\\./)+|/|http://)([^\\s])+\\.("+WebInfo.FilterUrl+")(\\?[^\\s^'^\">]+)?(\"\\s|'\\s)?";

		public Connect() {

			flag = true;
			website = WebInfo.WebSite;
			startPage=WebInfo.StartPage;
			MaxThread = WebInfo.MaxThread;
			MaxDapth = WebInfo.SearchDepth;			
			getimg = new Vector<GetImg>(MaxThread - 1);
			
			append("开始搜索资源");
			
			if (!Check.checkSavePathExists()) {
				flag = false;
				append("保存目录创建失败,程序终止。");
			}

			try {
				
				web = new URL(website);
				url = new URL(startPage);
				
			} catch (MalformedURLException me) {
				flag = false;
				append("没有找到网站");

			}
			
		}
			
		//启动下载函数
		
		public void run(){
			if(flag){
				append("搜索网站:"+web.toString());
	            append("当前网页:"+url.toString());
	            
	            
	            GetImg gi=new GetImg(getHTML(url),web,Main.this);
 			    getimg.add(gi);
 			    gi.start();
 			   
	            run(url,MaxDapth);
	            
	            
	            while(getThreadStillRuning()>=0){
	            	try{   

	            		append("正在下载图片,请稍候...");
	            	    Thread.sleep(3000);
	            	    
	            	}catch(InterruptedException e){
	            		append("程序被打断");
	            		break;
	            	}
	            }
	            
	            
	            append("---------------------------------------------------------------------------------------");
	            switch(GetImg.getImageCount()){	            	
	            case 0:append("找到"+savedURL.size()+" 个网站,没下载到图片,要努力呀。");break;
	            case 1:
	            case 2:
	            case 3:
	            case 4:
	            case 5:
	            case 6:
	            case 7:
	            case 8:
	            case 9:append("找到"+savedURL.size()+" 个网站,下载图片数量:"+GetImg.getImageCount()+ " 个 没有突破10个,加油啊。");break;
	            default:append("找到"+savedURL.size()+" 个网站,下载图片数量:"+GetImg.getImageCount()+ " 个 收获不小啊。");
	            }
	            append("---------------------------------------------------------------------------------------");
	            
	            
	            
	            GetImg.clearImageCount();
	            getimg.clear();
	            getimg=null;
	            savedURL.clear();
	            savedURL=null;
	            flag=false;
	            Main.this.setDownLoadSetEnable(true);
	            
			}
		}
		

		
		//搜索资源 并下载图片
		
		public void run(URL s,int depth){
			
			if(depth-->0){
				 Vector<WebPath> src=null;
			        src=getURL(getHTML(s),web);
			        if(src!=null){
			           
			    	   for (int i=0;i<src.size();i++){
			    		   

			    		   CurrentPage= getHTML(src.get(i).getURL());
			    		   
			    		   if(CurrentPage==null)
			    		        continue;
			    		   
			    		   if(ActiveThreadCount<MaxThread){
			    			   append("当前网页:"+src.get(i).getURLToString()); 		
			    			   ActiveThreadCount++;
			        		   GetImg gi=new GetImg(CurrentPage.trim(),web,Main.this);
			    			   getimg.add(gi);
			    			   gi.start();
			    		   }
			    		   else{
			    			   
			    			while(true){
			    				
					    	if(!Main.this.hasStart){
					    			   return;
					    	}
					    	
			    			 int cur=getThreadNotRuning();
			    			   if(cur>=0){
			    				   
			    				   getimg.remove(cur);
				        		   GetImg gi=new GetImg(CurrentPage.trim(),web,Main.this);
				        		   getimg.add(cur,gi);
			    				   gi.start();			    				   
			    				   break;
			    				   
			    			   }else{
			    				   try{
			    					   
			    				   Thread.sleep(2000);
			    				   
			    				   }catch(InterruptedException e){
			    					   append("线程被打断");
			    					   continue;
			    				   }
			    			   }
			    			   
			    			   }
			    			  
			    		   }
			    		   
			    		   if(!Main.this.hasStart){
			    			   return;
			    		   }
			    		   
			    		   run(src.get(i).getURL(),depth);  
			    		   
			    	   }
			    	src.removeAllElements();
			    	src=null;
			       }
				}
			
			
		}
		
		
		//得到失效线程并重新启动
		
			private int getThreadNotRuning(){
				
				if(getimg!=null){
					for(int i=0;i<MaxThread;i++){
					
						if(!getimg.get(i).isAlive()){
							append("第"+(i+1)+"个线程失效。");
							return i;
						}
						
					}
					return -2;
				}else
					return -1;
			}
			
      //检查线程是否全部失效
			
			private int getThreadStillRuning(){
				
				if(getimg!=null){
					for(int i=0;i<getimg.size();i++){
					
						if(getimg.get(i).isAlive()){
							return i;
						}
						
					}
					return -2;
				}else
					return -1;
				
			}
			
			private Vector<WebPath> getURL(String s,URL path){
				
        		
				if (s==null)
					return null;
				else{
					Vector<WebPath> src=new Vector<WebPath>();
					
					Pattern p=Pattern.compile(regex_html,Pattern.CASE_INSENSITIVE);
					Matcher m=p.matcher(s);
					
					while(m.find()){
						
		        		if(!Main.this.hasStart){
		    			     break;
		    		     }
						
						String href=m.group();
						
						href=href.substring(href.indexOf("href=")+5,href.length());
						href=href.replaceAll("\"", "");
						href=href.replaceAll("'", "");
						href=href.trim();
						
						String temp=WebInfo.getCurrentURL(href);
						
						if(temp!=null){
						  if(savedURL.get(temp)==null || !savedURL.get(temp)){	
							  
							  savedURL.put(temp, true);
						      WebPath wp=new WebPath(href,path);
						      src.add(wp);
						  
						  }else{
							  append("找到重复网站,继续搜索。"+href);
						  }
						}
					
					}		
					return src;
					
				}
				
			}
			
			
			public String getHTML(URL s){       // 获取指定URL的网页,返回网页内容
				
        		if(!Main.this.hasStart){
    			    return null;
    		     }
				
				DataInputStream dis=null;
				BufferedReader in=null;
				try {

					URL url= s;
					
					URLConnection connect = url.openConnection();					
					dis = new DataInputStream(connect.getInputStream());
					in = new BufferedReader(new InputStreamReader(dis,encode));
					String html = "";
					String readLine = null;
					while ((readLine = in.readLine()) != null) {
						html = html + readLine;
					}
					readLine=null;
					
					dis.close();
					in.close();
					return html;
					
				} catch (Exception ioe) {
					try{
						if(dis!=null)dis.close();
						if(in!=null)in.close();
						}catch(IOException eve){
							append("关闭连接错误");
							dis=null;
							in=null;
							return null;
						}
					append("没有找到网站");
					return null;
				}
			}

	}

	/////////////////////////////////////////////////////////////////////////////////////
	
    //	主函数
	
	public static void main(String[] args) {

		Main main = new Main();

	}

}



⌨️ 快捷键说明

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