用java实现的获取优酷等视频缩略图的实现代码
作者:
想要php版的朋友可以到这里下载测试 https://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
*/
}
}