java调用js文件

使用java调用js的方法

	    /**
	     * 调用js文件获取url
	     * @param video_id
	     * @return
	     * @throws ScriptException
	     * @throws FileNotFoundException
	     * @throws NoSuchMethodException
	     */
	    private static String excuteJs(String video_id) throws ScriptException,
	            FileNotFoundException, NoSuchMethodException {
	        ScriptEngineManager engineManager = new ScriptEngineManager();
	        ScriptEngine engine = engineManager.getEngineByName("JavaScript"); // 得到脚本引擎
	        String reader = null;
	        //获取文件所在的相对路径
	        //String text = System.getProperty("user.dir");
	        //reader = text + "\\src\\main\\resources\\test.js";
	         String path = ShortVideo.class.getResource("/").getPath();
        	reader = path + "/xgVideo.js"; 
        	FileReader fReader = new FileReader(reader);
	        engine.eval(fReader );
	        
	        Invocable inv = (Invocable) engine; 
	        //调用js中的方法
	        Object test2 = inv.invokeFunction("crc32", video_id);  
	        String url = test2.toString();
	        try{
				 fReader.close();
			}catch{
	        }
	       
	        return url;
	    }

所调用的js文件

function crc32(video_id) {
        var n = function() {
            for (var t = 0,
            e = new Array(256), n = 0; 256 != n; ++n) t = n,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
            e[n] = t;
            return "undefined" != typeof Int32Array ? new Int32Array(e) : e
        } (),
        o = function(t) {
            for (var e, o, r = -1,
            i = 0,
            a = t.length; i < a;) e = t.charCodeAt(i++),
            e < 128 ? r = r >>> 8 ^ n[255 & (r ^ e)] : e < 2048 ? (r = r >>> 8 ^ n[255 & (r ^ (192 | e >> 6 & 31))], r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & e))]) : e >= 55296 && e < 57344 ? (e = (1023 & e) + 64, o = 1023 & t.charCodeAt(i++), r = r >>> 8 ^ n[255 & (r ^ (240 | e >> 8 & 7))], r = r >>> 8 ^ n[255 & (r ^ (128 | e >> 2 & 63))], r = r >>> 8 ^ n[255 & (r ^ (128 | o >> 6 & 15 | (3 & e) << 4))], r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & o))]) : (r = r >>> 8 ^ n[255 & (r ^ (224 | e >> 12 & 15))], r = r >>> 8 ^ n[255 & (r ^ (128 | e >> 6 & 63))], r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & e))]);
            return r ^ -1
        },
        r = "/video/urls/v/1/toutiao/mp4/"+video_id + "?r=" + Math.random().toString(10).substring(2);
        "/" != r[0] && (r = "/" + r);
        var i = o(r) >>> 0;
        return ("https://ib.365yg.com"+r + "&s=" + i)
    }

你可能感兴趣的:(java)