想獲取優(yōu)酷等視頻縮略圖,在網(wǎng)上沒(méi)有找到滿意的資料,參考了huangdijia的PHP版工具一些思路,寫(xiě)了下面的JAVA版代碼。。其實(shí)也可以做成JS版的。
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.net.MalformedURLException; 5 import java.net.URL; 6 7 import net.sf.json.*; 8 9 10 public class test2 { 11 12 /** 13 * @param args 14 * @throws IOException 15 */ 16 public static void main(String[] args) throws IOException { 17 // TODO Auto-generated method stub 18 String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html"; 19 //獲取id后面的字符串,也就是"XMjU0MjI2NzY0.html" 20 int no = url.indexOf("id_"); 21 //獲取id值,也就是"XMjU0MjI2NzY0" 22 String videoId = url.substring(no+3, url.indexOf(".html")); 23 //獲取視頻信息數(shù)據(jù)的URL對(duì)象 24 URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3"); 25 //從URL對(duì)象中獲取輸入流 26 InputStreamReader isr = new InputStreamReader(myurl.openStream()); 27 //封裝 28 BufferedReader br = new BufferedReader(isr); 29 //readLine獲取文本 30 String urls = br.readLine(); 31 //關(guān)閉流 32 br.close(); 33 //獲取json對(duì)象 34 JSONObject json = JSONObject.fromObject(urls); 35 //獲取json數(shù)據(jù)(data內(nèi)) 36 JSONArray arr = json.getJSONArray("data"); 37 //獲取logo的值并打印 38 System.out.println(JSONObject.fromObject(arr.get(0)).get("logo")); 39 } 40 41 }
另一種沒(méi)有用json的方案,比較小白,也是本人第一次的方法。
1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.net.URL;
5
6
7
8 public class test1 {
9
10 /**
11 * @param args
12 * @throws IOException
13 */
14 public static void main(String[] args) throws IOException {
15 // TODO Auto-generated method stub
16 //設(shè)定視頻地址
17 String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
18 //獲取id后面的字符串,也就是"XMjU0MjI2NzY0.html"
19 int no = url.indexOf("id_");
20 //獲取id值,也就是"XMjU0MjI2NzY0"
21 String videoId = url.substring(no+3, url.indexOf(".html"));
22 //獲取視頻信息數(shù)據(jù)的URL對(duì)象
23 URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
24 //從URL對(duì)象中獲取輸入流
25 InputStreamReader isr = new InputStreamReader(myurl.openStream());
26 //封裝
27 BufferedReader br = new BufferedReader(isr);
28 //readLine獲取文本,然后以"\/"切割文本中的信息
29 String[] urls = br.readLine().split("\\/");
30 isr.close();
31 br.close();
32 //因?yàn)樾畔?shù)據(jù)是固定格式的,所以直接第四個(gè)元素肯定是我們所需要的。
33 String target = urls[3];
34 //對(duì)上面結(jié)果繼續(xù)以"切割
35 String imgid = target.substring(0,target.indexOf("\""));
36 //拼湊一下,就是我們需要的地址了
37 String img = "http://g2.ykimg.com/" + imgid;
38 System.out.println(img);
39 /*
40 結(jié)果為:http://g2.ykimg.com/1100641F464D8FBF5FA3D90209C8DF96B67E2C-567E-AB53-C132-D7787FC966AB
41 */
42 }
43
44 }