工具类

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Tools {
    public static String ExcuteJs(String jsSource, String method,
            Object... paras) throws ScriptException, FileNotFoundException,
            NoSuchMethodException, UnsupportedEncodingException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("JavaScript"); // 得到脚本引擎
        // Reader scriptReader = new InputStreamReader(new
        // FileInputStream("html.html"),"utf-8");
        engine.eval(jsSource);
        Invocable inv = (Invocable) engine;
        Object a = inv.invokeFunction(method, paras);
        return a.toString();
    }

    // public static String ExcuteJs1(Object... paras) throws ScriptException,
    // FileNotFoundException, NoSuchMethodException,
    // UnsupportedEncodingException
    // {
    //
    // ScriptEngineManager engineManager = new ScriptEngineManager();
    // ScriptEngine engine = engineManager.getEngineByName("JavaScript");
    // //得到脚本引擎
    // Reader scriptReader = new InputStreamReader(new
    // FileInputStream("html.html"),"utf-8");
    // engine.eval(scriptReader);
    // Invocable inv = (Invocable)engine;
    // Object a = inv.invokeFunction("encryptPassword", paras);
    // return a.toString();
    // }

    public static String QuMiddle(String str, String str1, String str2) {
        try {
            int leftlocation;// 左边位置
            int rightlocation;// 右边位置
            leftlocation = str.indexOf(str1);
            if (str1 == str2) {
                rightlocation = str.indexOf(str2, 1);
            } else {
                rightlocation = str.indexOf(str2);
            }
            return str.substring(leftlocation + str1.length(), rightlocation);
        } catch (Exception e) {
        }
        return "";
    }

    public static String readFile(String path, String charset) {
        BufferedReader reader = null;
        String str = "";
        String temp = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), charset));
            while ((temp = reader.readLine()) != null) {
                str = str + temp+"\r\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e2) {
            }
        }
        return str;
    }
}

 

posted @ 2014-07-26 00:57  宝贝,我永远都在  阅读(88)  评论(0)    收藏  举报