用java实现的获取优酷等视频缩略图的实现代码
时间:2022-03-11 14:19:32
想要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
*/
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 使用异步编程的方法
![](https://img.aspxhome.com/file/2023/7/113777_0s.png)
很简单的Java断点续传实现原理
![](https://img.aspxhome.com/file/2023/8/80828_0s.jpg)
Java中为什么start方法不能重复调用而run方法可以?
![](https://img.aspxhome.com/file/2023/8/59598_0s.webp)
Java中逆序遍历List集合的实现
![](https://img.aspxhome.com/file/2023/4/62114_0s.png)
Java实现发送手机短信语音验证功能代码实例
Android编程调用Camera和相册功能详解
C#和Java有什么区别和联系
C#使用Socket实现局域网聊天
![](https://img.aspxhome.com/file/2023/7/119467_0s.jpg)
Java循环结构之多重循环及continue break
关于feign.codec.DecodeException异常的解决方案
![](https://img.aspxhome.com/file/2023/7/118897_0s.png)
java实现简易飞机大战
![](https://img.aspxhome.com/file/2023/7/81227_0s.jpg)
详解WPF中的隧道路由和冒泡路由事件
![](https://img.aspxhome.com/file/2023/8/68068_0s.png)
基于Java文件输入输出流实现文件上传下载功能
![](https://img.aspxhome.com/file/2023/0/58170_0s.png)
C#之CLR内存深入分析
![](https://img.aspxhome.com/file/2023/4/108764_0s.png)
Android中ActionBar以及menu的代码设置样式
![](https://img.aspxhome.com/file/2023/2/106032_0s.png)
Java中使用HashMap时指定初始化容量性能解析
Android提高之使用NDK把彩图转换灰度图的方法
![](https://img.aspxhome.com/file/2023/6/88686_0s.jpg)
Android实现简单实用的垂直进度条
![](https://img.aspxhome.com/file/2023/9/96749_0s.jpg)
Java包装类原理与用法实例分析
![](https://img.aspxhome.com/file/2023/9/83429_0s.png)