package action;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import com.sun.deploy.net.HttpRequest;
import org.apache.struts2.ServletActionContext;
import org.python.antlr.ast.Str;
import web特有.webTools.HibernateTools;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

public class translate extends HibernateTools {
    private String key="欢迎使用由高金磊设计的翻译网站";
    public String result="";
    //必须使用绝对路径
    String filepath=getClassPath()+"/action/translation.py";

    public String getresult()throws Exception{
        String exe = "python";
        String command = filepath;
        String[] cmdArr = new String[] {exe, command, key};
        Process process = Runtime.getRuntime().exec(cmdArr);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"gbk"));
        process.waitFor();
        result=reader.readLine();
        HttpServletRequest request = ServletActionContext.getRequest();
        //去掉多于引号兼容前端js
        request.setAttribute("result", result.replace("\"", ""));
        request.setAttribute("key", key.replace("\"", ""));
        return SUCCESS;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

//    public static void main(String arg[]){//main--来自高金磊
//
//        try {
//            System.out.println(new translate().getresult("你真好"));
//            System.out.println(new translate().getresult("That's very kind of you"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    private  String getClassPath(){
        String path="";
        try {
            path=new File(getClass().getClassLoader().getResource("").toURI()).getPath();
        }catch (URISyntaxException ex) {}
        return path;
    }

}
最近下载更多
Trickster  LV9 2022年5月17日
啦啦啦0321  LV1 2022年3月21日
SupreLuc  LV1 2021年5月14日
clscls  LV1 2019年10月16日
232454  LV8 2019年10月11日
zhangguobin  LV14 2019年10月8日
and_zej  LV11 2019年10月7日
aihui523  LV34 2019年10月4日
最代码官方  LV167 2019年9月30日
最近浏览更多
2017143155  LV12 2023年6月27日
Dominick  LV14 2023年4月3日
zhoujianyi  LV3 2022年12月13日
蛇头凌志  LV8 2022年8月22日
JavaPro_Allen  LV9 2022年6月10日
Trickster  LV9 2022年5月17日
啦啦啦0321  LV1 2022年3月21日
329512801  LV28 2022年2月22日
wanglinddad  LV54 2022年1月25日
Jyying  LV2 2022年1月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友