干成一桩大事 ServletWindowsCache.net框架
程序员文章站
2024-03-18 19:12:22
...
我是豆瓣的会员,又用的是MSN Spaces的BLOG,目前用得还不错,都不打算退出。但是豆瓣提供的那个豆瓣秀服务,在MSN Spaces上用不成
没办法,只能自己动手了。实际效果,见:http://spaces.msn.com/members/zbw25/
1、搞一个Custom HTML
2、在公司的服务器上,装一个软件,ImageMagick。下载地址:http://www.imagemagick.org/download/binaries/ImageMagick-6.2.5-4-Q16-windows-dll.exe
3、下载一个JMagick。下载地址:
http://www.yeo.id.au/jmagick/quickload/win-6.2.4/jmagick-6.2.4-1-IM-6.2.5.zip
4、将dll文件,copy到Windows的System32目录下。将jar文件copy到某lib目录下。
5、写一个Servlet,代码如下:
1 package tv.recool.misc;
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11
12 import magick.ImageInfo;
13 import magick.MagickImage;
14 import magick.MontageInfo;
15
16 public class PutBooks extends HttpServlet {
17
18 //根据url,取得图片的byte[]
19 private static byte[] getImageBytes(String url){
20 try {
21 return StreamUtils.read((new URL(url)).openStream());
22 } catch (Exception e) {
23 return null;
24 }
25 }
26
27 //根据url,取得文字
28 private static String getImageText(String url){
29 try {
30 URL u=new URL(url);
31 URLConnection uc=u.openConnection();
32
33 //要做足功夫,不然豆瓣就会拒绝你的请求
34 uc.setRequestProperty("Host","www.douban.com");
35 uc.setRequestProperty("Accept","*/*");
36 uc.setRequestProperty("Referer","http://www.douban.com/");
37 uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
38 uc.setRequestProperty("Pragma","no-cache");
39 uc.setRequestProperty("Cache-Control","no-cache");
40 uc.setRequestProperty("Connection","close");
41
42 //StreamUtils是一个辅助工具,就不再给出了
43 return new String(StreamUtils.read(uc.getInputStream()),"utf-8");
44 } catch (Exception e) {
45 return "";
46 }
47 }
48
49 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
50 //取得参数
51 String uid=req.getParameter("uid");
52 String n=req.getParameter("n");
53 String columns=req.getParameter("columns");
54 String select=req.getParameter("select");
55 String picsize=req.getParameter("picsize")+"";
56 String show=req.getParameter("show");
57
58 try {
59 //设置JMagick的ClassLoader,否则会报错
60 System.setProperty("jmagick.systemclassloader","no");
61
62 String str= getImageText("http://www.douban.com/service/badge/"+
63 uid+"/?show="+show
64 +"&select="+select
65 +"&n="+n
66 +"&picsize="+picsize
67 +"&columns="+columns);
68
69 int getSize=Integer.parseInt(n);
70 int colSize=Integer.parseInt(columns);
71 int len=getSize/colSize;
72
73 //申请一个数组
74 MagickImage images[] = new MagickImage[getSize];
75
76 //用最简单的办法解析字符串,因为豆瓣的输出是固定的
77 String imgstr[]=str.split(".jpg");
78
79 for(int i=0;i<getSize;i++){
80 if(i<imgstr.length){
81 String picurl="";
82
83 //根据图片大小的设定,解析有所不同
84 if(picsize.equals("medium")){
85 String temps[]=imgstr[i].split("http://www.douban.com/mpic/");
86 picurl="http://www.douban.com/mpic/"+temps[1]+".jpg";
87 } else {
88 String temps[]=imgstr[i].split("http://www.douban.com/spic/");
89 picurl="http://www.douban.com/spic/"+temps[1]+".jpg";
90 }
91 MagickImage image = new MagickImage(new ImageInfo(),getImageBytes(picurl));
92 images[i]=image;
93 }
94 }
95
96 //建立一个图片组
97 MagickImage seqImage = new MagickImage(images);
98
99 //建立一个图片框架
100 MontageInfo montageInfo = new MontageInfo(new ImageInfo());
101
102 //setGeometry(String)设置图片的几何特征
103 if(picsize.equals("medium")){
104 montageInfo.setGeometry("99x149+1+1>");
105 } else {
106 montageInfo.setGeometry("66x99+1+1>");
107 }
108
109 //NxM的一个表格
110 montageInfo.setTile(colSize+"x"+len);
111 montageInfo.setBorderWidth(0);
112
113 //得到一张组合后的图片
114 MagickImage montage = seqImage.montageImages(montageInfo);
115 montage.writeImage(new ImageInfo());
116 montage.setMagick("JPG");
117
118 //输出到byte[]
119 byte data[]=montage.imageToBlob(new ImageInfo());
120
121 //通过Servlet输出
122 res.setContentType("image/*");
123 OutputStream toClient=res.getOutputStream();
124 toClient.write(data);
125 toClient.close();
126 }
127 catch (Exception ex) {
128 }
129 }
130 }
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11
12 import magick.ImageInfo;
13 import magick.MagickImage;
14 import magick.MontageInfo;
15
16 public class PutBooks extends HttpServlet {
17
18 //根据url,取得图片的byte[]
19 private static byte[] getImageBytes(String url){
20 try {
21 return StreamUtils.read((new URL(url)).openStream());
22 } catch (Exception e) {
23 return null;
24 }
25 }
26
27 //根据url,取得文字
28 private static String getImageText(String url){
29 try {
30 URL u=new URL(url);
31 URLConnection uc=u.openConnection();
32
33 //要做足功夫,不然豆瓣就会拒绝你的请求
34 uc.setRequestProperty("Host","www.douban.com");
35 uc.setRequestProperty("Accept","*/*");
36 uc.setRequestProperty("Referer","http://www.douban.com/");
37 uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
38 uc.setRequestProperty("Pragma","no-cache");
39 uc.setRequestProperty("Cache-Control","no-cache");
40 uc.setRequestProperty("Connection","close");
41
42 //StreamUtils是一个辅助工具,就不再给出了
43 return new String(StreamUtils.read(uc.getInputStream()),"utf-8");
44 } catch (Exception e) {
45 return "";
46 }
47 }
48
49 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
50 //取得参数
51 String uid=req.getParameter("uid");
52 String n=req.getParameter("n");
53 String columns=req.getParameter("columns");
54 String select=req.getParameter("select");
55 String picsize=req.getParameter("picsize")+"";
56 String show=req.getParameter("show");
57
58 try {
59 //设置JMagick的ClassLoader,否则会报错
60 System.setProperty("jmagick.systemclassloader","no");
61
62 String str= getImageText("http://www.douban.com/service/badge/"+
63 uid+"/?show="+show
64 +"&select="+select
65 +"&n="+n
66 +"&picsize="+picsize
67 +"&columns="+columns);
68
69 int getSize=Integer.parseInt(n);
70 int colSize=Integer.parseInt(columns);
71 int len=getSize/colSize;
72
73 //申请一个数组
74 MagickImage images[] = new MagickImage[getSize];
75
76 //用最简单的办法解析字符串,因为豆瓣的输出是固定的
77 String imgstr[]=str.split(".jpg");
78
79 for(int i=0;i<getSize;i++){
80 if(i<imgstr.length){
81 String picurl="";
82
83 //根据图片大小的设定,解析有所不同
84 if(picsize.equals("medium")){
85 String temps[]=imgstr[i].split("http://www.douban.com/mpic/");
86 picurl="http://www.douban.com/mpic/"+temps[1]+".jpg";
87 } else {
88 String temps[]=imgstr[i].split("http://www.douban.com/spic/");
89 picurl="http://www.douban.com/spic/"+temps[1]+".jpg";
90 }
91 MagickImage image = new MagickImage(new ImageInfo(),getImageBytes(picurl));
92 images[i]=image;
93 }
94 }
95
96 //建立一个图片组
97 MagickImage seqImage = new MagickImage(images);
98
99 //建立一个图片框架
100 MontageInfo montageInfo = new MontageInfo(new ImageInfo());
101
102 //setGeometry(String)设置图片的几何特征
103 if(picsize.equals("medium")){
104 montageInfo.setGeometry("99x149+1+1>");
105 } else {
106 montageInfo.setGeometry("66x99+1+1>");
107 }
108
109 //NxM的一个表格
110 montageInfo.setTile(colSize+"x"+len);
111 montageInfo.setBorderWidth(0);
112
113 //得到一张组合后的图片
114 MagickImage montage = seqImage.montageImages(montageInfo);
115 montage.writeImage(new ImageInfo());
116 montage.setMagick("JPG");
117
118 //输出到byte[]
119 byte data[]=montage.imageToBlob(new ImageInfo());
120
121 //通过Servlet输出
122 res.setContentType("image/*");
123 OutputStream toClient=res.getOutputStream();
124 toClient.write(data);
125 toClient.close();
126 }
127 catch (Exception ex) {
128 }
129 }
130 }
6、在MSN Spaces的Custom HTML中,写以下HTML:
<a href="http://www.douban.com/people/1019112/"><img src="http://www.recool.tv:8080/Recool-WebServer/putBooks?uid=1019112&show=collection&select=random&n=12&columns=3" border=0 width=178></a>
7、搞定,收工。