想要php版的朋友可以到這裡下載測試 http://www.jb51.net/codes/83179.html
代碼如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.*;
public class test2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
//獲取id後面的字符串,也就是"XMjU0MjI2NzY0.html"
int no = url.indexOf("id_");
//獲取id值,也就是"XMjU0MjI2NzY0"
String videoId = url.substring(no+3, url.indexOf(".html"));
//獲取視頻信息數據的URL對象
URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
//從URL對象中獲取輸入流
InputStreamReader isr = new InputStreamReader(myurl.openStream());
//封裝
BufferedReader br = new BufferedReader(isr);
//readLine獲取文本
String urls = br.readLine();
//關閉流
br.close();
//獲取json對象
JSONObject json = JSONObject.fromObject(urls);
//獲取json數據(data內)
JSONArray arr = json.getJSONArray("data");
//獲取logo的值並打印
System.out.println(JSONObject.fromObject(arr.get(0)).get("logo"));
}
}
另一種沒有用json的方案,比較小白,也是本人第一次的方法。
代碼如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class test1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//設定視頻地址
String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
//獲取id後面的字符串,也就是"XMjU0MjI2NzY0.html"
int no = url.indexOf("id_");
//獲取id值,也就是"XMjU0MjI2NzY0"
String videoId = url.substring(no+3, url.indexOf(".html"));
//獲取視頻信息數據的URL對象
URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
//從URL對象中獲取輸入流
InputStreamReader isr = new InputStreamReader(myurl.openStream());
//封裝
BufferedReader br = new BufferedReader(isr);
//readLine獲取文本,然後以"\/"切割文本中的信息
String[] urls = br.readLine().split("\\/");
isr.close();
br.close();
//因為信息數據是固定格式的,所以直接第四個元素肯定是我們所需要的。
String target = urls[3];
//對上面結果繼續以"切割
String imgid = target.substring(0,target.indexOf("\""));
//拼湊一下,就是我們需要的地址了
String img = "http://g2.ykimg.com/" + imgid;
System.out.println(img);
/*
結果為:http://g2.ykimg.com/1100641F464D8FBF5FA3D90209C8DF96B67E2C-567E-AB53-C132-D7787FC966AB
*/
}
}