With Java to achieve youku and other video thumbnail realization code

  • 2020-04-01 01:57:39
  • OfStack

Those who want a PHP version can download the test here (link: #)


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 {
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
        //Get the string after the id, which is "xmju0mji2nzy0.html"
        int no = url.indexOf("id_");
        //Get the id value, which is "XMjU0MjI2NzY0"
        String videoId = url.substring(no+3, url.indexOf(".html"));
        //Gets the URL object of the video information data
        URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
  
        //Gets the input stream from the URL object
        InputStreamReader isr = new InputStreamReader(myurl.openStream());
        //encapsulation
        BufferedReader br = new BufferedReader(isr);
        //ReadLine gets the text
        String urls = br.readLine();
        //Close the stream
        br.close();
        //Get the json object
        JSONObject json = JSONObject.fromObject(urls);
        //Get json data (inside data)
        JSONArray arr = json.getJSONArray("data");
        //Get the value of the logo and print it
        System.out.println(JSONObject.fromObject(arr.get(0)).get("logo"));
    }
}

Another method without json, relatively white, is my first method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class test1 {
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        //Set video address
        String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
        //Get the string after the id, which is "xmju0mji2nzy0.html"
        int no = url.indexOf("id_");
        //Get the id value, which is "XMjU0MjI2NzY0"
        String videoId = url.substring(no+3, url.indexOf(".html"));
        //Gets the URL object of the video information data
        URL myurl = new URL("http://v.youku.com/player/getPlayList/VideoIDS/"+videoId+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3");
        //Gets the input stream from the URL object
        InputStreamReader isr = new InputStreamReader(myurl.openStream());
        //encapsulation
        BufferedReader br = new BufferedReader(isr);
        //ReadLine takes the text and then cuts the information in the text with "/"
        String[] urls = br.readLine().split("\/");
        isr.close();
        br.close();
        //Because the information data is in a fixed format, the direct fourth element must be what we need.
        String target = urls[3];
        //Continue to "cut" the above results
        String imgid = target.substring(0,target.indexOf("""));
        //Piece it together and it's the address we need
        String img = "http://g2.ykimg.com/" + imgid;
        System.out.println(img);
        /*
          The result is: http://g2.ykimg.com/1100641F464D8FBF5FA3D90209C8DF96B67E2C-567E-AB53-C132-D7787FC966AB
         */
    }
}


Related articles: