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

📄 getgraphics.java

📁 网络实验参考资料,希望对大家有点用,也是希望资源共享啊
💻 JAVA
字号:
/** GetGraphics - walk through a Web site, find
 * all the graphics and save in a local directory tree
 * 
 * Sample program for Arachnid web spider framework
 */

import java.io.*;
import java.net.*;
import java.util.*;
import bplatt.spider.*;

public class GetGraphics {
	
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("java GetGraphics <url> <output directory>");
			System.exit(-1);
		}
		File outdir = new File(args[1]);
		if (outdir.isDirectory()  == false || outdir.canWrite() == false) {
			System.out.println("Cannot access directory "+args[1]);
			System.exit(-1);
		}
		ImageSpider spider = null;
		try { spider = new ImageSpider(args[0],outdir); }
		catch(MalformedURLException e) {
			System.out.println(e);
			System.out.println("Invalid URL: "+args[0]);
			System.exit(-1);
		}
		System.out.println("Get Graphics:");
		spider.traverse();
		System.out.println("Finished");
	}
}
	
class ImageSpider extends Arachnid {
	private HashSet images;
	private File outdir;
	
	public ImageSpider(String base, File outdir) throws MalformedURLException
	{
		super(base);
		super.setDelay(5);
		images = new HashSet();
		this.outdir = outdir;
	}
	protected void handleBadLink(URL url,URL parent, PageInfo p) { }
	protected void handleBadIO(URL url, URL parent) { }
		
	protected void handleLink(PageInfo p) {
		URL[] list = p.getImages();
		if (list != null) {
			for (int i=0; i<list.length; ++i) {
				if (images.contains(list[i]) == false) {
					images.add(list[i]);
					if (saveImage(list[i],outdir))
						System.out.println("Saved image from: " + list[i].toString());
					else System.out.println("Could not save image: " + list[i].toString());
				}
			}
		}
	}
	protected void handleNonHTMLlink(URL url, URL parent,PageInfo p) { }
		
	protected void handleExternalLink(URL url, URL parent) { }
	
	private boolean saveImage(URL url, File dir)
	{
		String outdir = dir.toString();
		String file = url.getFile();
		File outfile;
		if (outdir == null || file == null || outdir.length() == 0 ||
			file.length() == 0) return(false);
		if (File.separatorChar == '\\') {
			StringBuffer b = new StringBuffer(file);
			for (int i=0; i<b.length(); ++i)
				if (b.charAt(i) == '/') b.setCharAt(i,'\\');
			file = b.toString();
		}
		if (outdir.charAt(outdir.length()-1) == File.separatorChar ||
			file.charAt(0) == File.separatorChar) outfile = new File(outdir+file);
		else outfile = new File(outdir+File.separatorChar+file);
		
		// Create any needed intermediate directories
		if (outfile.getParent() != null) {
			File parentDir = outfile.getParentFile();
			if (parentDir.exists() == false) parentDir.mkdirs();
		}
		
		byte[] theImage = this.getContent(url);
		if (theImage != null) {
			int size = theImage.length;
			try {
				BufferedOutputStream fstream = 
					new BufferedOutputStream(new FileOutputStream(outfile));
				fstream.write(theImage,0,size);
				fstream.flush();
				fstream.close();
			}
			catch(IOException e) { return(false); }
			return(true);
		}
		else return(false);
	}
}

⌨️ 快捷键说明

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