萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> java獲取優酷等視頻縮略圖

java獲取優酷等視頻縮略圖

   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 //獲取視頻信息數據的URL對象

  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對象中獲取輸入流

  26 InputStreamReader isr = new InputStreamReader(myurl.openStream());

  27 //封裝

  28 BufferedReader br = new BufferedReader(isr);

  29 //readLine獲取文本

  30 String urls = br.readLine();

  31 //關閉流

  32 br.close();

  33 //獲取json對象

  34 JSONObject json = JSONObject.fromObject(urls);

  35 //獲取json數據(data內)

  36 JSONArray arr = json.getJSONArray("data");

  37 //獲取logo的值並打印

  38 System.out.println(JSONObject.fromObject(arr.get(0)).get("logo"));

  39 }

  40

  41 }

  另一種沒有用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 //設定視頻地址

  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 //獲取視頻信息數據的URL對象

  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對象中獲取輸入流

  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 //因為信息數據是固定格式的,所以直接第四個元素肯定是我們所需要的。

  33 String target = urls[3];

  34 //對上面結果繼續以"切割

  35 String imgid = target.substring(0,target.indexOf("""));

  36 //拼湊一下,就是我們需要的地址了

  37 String img = "http://g2.ykimg.com/" + imgid;

  38 System.out.println(img);

  39 /*

  40 結果為:http://g2.ykimg.com/1100641F464D8FBF5FA3D90209C8DF96B67E2C-567E-AB53-C132-D7787FC966AB

  41 */

  42 }

  43

  44 }

copyright © 萬盛學電腦網 all rights reserved