日期:2014-05-20  浏览次数:20716 次

java截取网站图片的问题
最近上头出了道难题,给出一个网站的地址,用java获取该网站的页面的压缩图,不知道能不能实现,若能,帮忙指点一下,谢谢

------解决方案--------------------
学习
------解决方案--------------------
//Call the Web page and convert to Image
BufferedImage ire;
ire = WebImage.create( " <web page URL> ", 800, 600);
//You can convert the BufferedImage to
any format that you wish, jpg I thought was the best format
ImageIO.write(ire, "jpg ", new File( "c:\\Temp\\tt.jpg "));


//Class that Converts the web page to Image
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public abstract class WebImage
{
static class Kit extends HTMLEditorKit
{
public Document createDefaultDocument() {
HTMLDocument doc =
(HTMLDocument) super.createDefaultDocument();
doc.setTokenThreshold(Integer.MAX_VALUE);
doc.setAsynchronousLoadPriority(-1);
return doc;
}
}

public static BufferedImage create
(String src, int width, int height) {
BufferedImage image = null;
JEditorPane pane = new JEditorPane();
Kit kit = new Kit();
pane.setEditorKit(kit);
pane.setEditable(false);
pane.setMargin(new Insets(0,0,0,0));
try {
pane.setPage(src);
image = new BufferedImage
(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
Container c = new Container();
SwingUtilities.paintComponent
(g, pane, c, 0, 0, width, height);
g.dispose();
} catch (Exception e) {
System.out.println(e);
}
return image;
}
}


------解决方案--------------------
其实不是很难,主要是体力活;
主要工作原理是分析页面,Connection此网站后,获得页面内容进行分析,把图片地址取出,get到本地(就以流的形式,存成文件)。再分析出其他的 <a href..... 一层层进去。就用递归吧。。
getPage() parse()...write()几个方法就够了代码不会超过80行。。。

努力吧。

------解决方案--------------------
学习
------解决方案--------------------
alexa就是有这个功能的。
------解决方案--------------------
孟子E章,今天看到你了,pfpf。
------解决方案--------------------
孟子E章,不是搞asp.net的吗,呵呵,
------解决方案--------------------
ire = WebImage.create( "http://dotnet.aspx.cc/ ", 800, 600);
------解决方案--------------------
不用递归的 用正则表达式就简单多了
------解决方案--------------------
总算基本看懂 孟子E章的代码了 学习一下
------解决方案--------------------
JEditorPane 不行
------解决方案--------------------
看懂楼主的意思了。就是像alexa的网页截图,不会,帮顶!