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