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;
}
}
最近下载更多
最近浏览更多
Adguard LV3
2024年9月15日
元气缘灭
2024年5月20日
暂无贡献等级
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日

