📄 googleimagekit.java
字号:
package com.aztsoft.games.util;
import com.aztsoft.games.hangman.HangmanApplet;
import java.util.Vector;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.io.*;
/**
* Sends a query to google images and strips the 20 images from the return page
* uses the safe=active filter
* take it off and the game gets tougher but dangerous to put publicly
*/
public class GoogleImageKit {
final static String searchString = "http://images.google.com/images?q=";
final static String googleMatchString = "img src=/images?";
private Vector images = new Vector();
private String pageurl = "";
public GoogleImageKit(String keyword) {
this.pageurl = searchString + keyword + "&safe=active";
}
public Vector fetchVectorOfImages() {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(pageurl);
URLConnection con = url.openConnection();
con.setRequestProperty
("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader webData = new BufferedReader(new InputStreamReader(con.getInputStream()));
for (; ;) {
int data = webData.read();
if (data == -1)
break;
else {
sb.append((char) data);
}
}
} catch (MalformedURLException mue) {
System.err.println("Invalid URL");
} catch (IOException ioe) {
System.err.println("I/O Error - " + ioe);
}
int pos = 0;
int tempPos = 0;
int counter = 0;
while ((tempPos = sb.indexOf(googleMatchString, pos)) > 0) {
int endPos = sb.indexOf(">", tempPos);
String imageUrl = sb.substring(tempPos, endPos - 1);
int spacePos = imageUrl.indexOf("width", 0);
if (spacePos <= 0) {
} else {
String ts = imageUrl.substring(8, spacePos);
String fixedURL = "http://images.google.com" + ts;
try {
images.add(HangmanApplet.hangmanApplet.getImage(new URL(fixedURL)));
} catch (MalformedURLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
pos = tempPos + imageUrl.length();
counter++;
}
return images;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -