Java 调用翻译软件实现英文文档翻译
2021-06-27 11:05
标签:tostring socket display epo trim 步骤 积分 ORC ringbuf 因最近要进行OCP的考试准备。看着大堆英文文档确实有些疼痛。又因文档内容有点大,又需要逐一去翻译 又很费时费力。于是 百度了一番,找到一些 可以使用Java来调用百度翻译软件的API( 注:(官方标注)每月前200万字不要钱,49元/月) 。于是就自己手动的修改了一番。然后就使用。 百度官方API 访问地址:http://api.fanyi.baidu.com/api/trans/product/apidoc 百度官方计费声明: 下面是Java调用百度API实现翻译的具体步骤: 申请地址申请的详见 点击打开链接 申请之后,会得到APP_ID和SECURITY_KEY BaiduTranslateDemo Main 注 :本文来源于《Java 破解谷歌翻译 免费 api 调用》 1
获取 tkk 调用如下: 输出: 代码如下 需要的js http://download.csdn.net/download/qq_35704236/10156433 但是 封ip 的速度太快了, 各位如果需要用于生产环境,必须加上ip
代理,如果实在不行。。。就换百度吧。。。虽然有字数限制。。 ——————————————- 2018-5-16
————————————————————- 应读者要求给出 Browser, 其实就是包装了下 http, 有需要的小伙伴可以直接下载上面的
jar包,如果积分不够 。。私聊楼主 Java 调用翻译软件实现英文文档翻译 标签:tostring socket display epo trim 步骤 积分 ORC ringbuf 原文地址:https://www.cnblogs.com/ios9/p/9651770.html前言:
Java调用百度API实现翻译
一、在写代码之前先在在百度翻译平台中,申请APP_ID
二、java代码如下
1:代码结构下图
2:主要代码如下:
package spring;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* 百度翻译引擎java示例代码
*/
public class BaiduTranslateDemo{
private static final String UTF8 = "utf-8";
//申请者开发者id,实际使用时请修改成开发者自己的appid
private static final String appId = "自己注册一个appid";
//申请成功后的证书token,实际使用时请修改成开发者自己的token
private static final String token = "认证证书信息";
private static final String url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
//随机数,用于生成md5值,开发者使用时请激活下边第四行代码
private static final Random random = new Random();
public String translate(String q, String from, String to) throws Exception{
//用于md5加密
//int salt = random.nextInt(10000);
//本演示使用指定的随机数为1435660288
int salt = 1435660288;
// 对appId+源文+随机数+token计算md5值
StringBuilder md5String = new StringBuilder();
md5String.append(appId).append(q).append(salt).append(token);
String md5 = DigestUtils.md5Hex(md5String.toString());
//使用Post方式,组装参数
HttpPost httpost = new HttpPost(url);
List
package spring;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 直接运行main方法即可输出翻译结果
*/
public class Main {
public static void main(String[] args) throws Exception {
// translateZhToEn();
// translateEnToZh();
translateTxtInfo();
}
/**
* 中文翻译为英文
*/
public static void translateZhToEn() {
String source = "百度翻译引擎示例";
String result;
try {
result = BaiduTranslateDemo.translateZhToEn(source);
if (result == null) {
System.out.println("翻译出错,参考百度错误代码和说明。");
return;
}
System.out.println(source + ":" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 英文翻译为中文
*/
public static void translateEnToZh() {
String source = "CREATE GLOBAL TEMPORARY TABLE report_work_area";
String result;
try {
result = BaiduTranslateDemo.translateEnToZh(source);
if (result == null) {
System.out.println("翻译出错,参考百度错误代码和说明。");
return;
}
System.out.println(source + ":" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取txt文件内容翻译为中文
*/
public static void translateTxtInfo() {
String sorceFilePath = "E:\\txtfile\\052.txt";
String resultFilePath = "E:\\\\txtfile\\\\test1.txt";
// 构建指定文件
File resultFile = new File(resultFilePath);
FileReader fr = null;
FileWriter fw = null;
BufferedWriter bw = null;
BufferedReader br = null;
OutputStream out = null;
String result;
String line = "";
try {
// 根据文件创建文件的输入流
fr = new FileReader(sorceFilePath);
br = new BufferedReader(fr);
fw = new FileWriter(new File(resultFilePath));
// 写入中文字符时会出现乱码
bw = new BufferedWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(new File(resultFilePath)), "UTF-8")));
// 根据文件创建文件的输出流
out = new FileOutputStream(resultFile);
// 创建字节数组
byte[] data = new byte[1024];
StringBuffer resultBuffer=new StringBuffer();
// 读取内容,放到字节数组里面
while ((line = br.readLine()) != null) {
String message = line.trim();
// 英文翻译为中文
if(message.trim()!=null&& !message.trim().equals("")) {
if(message.trim().indexOf("Answer")>-1) {
resultBuffer.append(message+"\t\n");
}else {
result = BaiduTranslateDemo.translateEnToZh( message.trim());
resultBuffer.append(message+"("+result+")"+"\t\n");
}
}
}
// 把内容转换成字节数组
byte[] resultdata = resultBuffer.toString().getBytes();
// 向文件写入内容
out.write(resultdata);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭输入流
br.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3:源码下载
Java 破解谷歌翻译 免费 api 调用
在公司大佬的指点下, 写了个破解谷歌翻译的工具类,能破解谷歌翻译, 思路如下:
2
根据 tkk,和 输入内容 获取 tk
3
根据 word,tk ,组装 url 访问 谷歌翻译 apipublic static void main(String[] args) {
/*GoogleApi googleApi = new GoogleApi();*/
GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128);
String result = googleApi.translate("Many applications within the enterprise domain ", "", "zh");
System.out.println(result);
}
企业领域内的许多应用程序
package org.trans;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URLEncoder;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONArray;
public class GoogleApi {
private static final String PATH = "/gettk.js";
static ScriptEngine engine = null;
private Browser browser = null;
static{
ScriptEngineManager maneger = new ScriptEngineManager();
engine = maneger.getEngineByName("javascript");
FileInputStream fileInputStream = null;
Reader scriptReader = null;
try{
scriptReader = new InputStreamReader(GoogleApi.class.getResourceAsStream(PATH), "utf-8");
engine.eval(scriptReader);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(scriptReader != null){
try {
scriptReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public GoogleApi(){
this.browser = new Browser();
}
public GoogleApi(String ip, Integer port){
this.browser = new Browser();
this.browser.setProxy(ip, port);
}
public String getTKK(){
browser.setUrl("https://translate.google.cn/");
try{
String result = browser.executeGet();
if(StringUtils.isNotBlank(result)){
if(result.indexOf("TKK") > -1){
String tkk = result.split("TKK")[1];
tkk = tkk.split("\\)\\;")[0];
tkk = tkk + ");";
tkk = tkk.substring(1, tkk.length());
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
return (String) engine.eval(tkk);
}
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static String getTK(String word, String tkk){
String result = null;
try{
if (engine instanceof Invocable){
Invocable invocable = (Invocable) engine;
result = (String) invocable.invokeFunction("tk", new Object[]{word, tkk});
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public String translate(String word, String from, String to){
if(StringUtils.isBlank(word)){
return null;
}
String _tkk = getTKK();
if(StringUtils.isBlank(_tkk)){
return null;
}
String _tk = getTK(word, _tkk);
try{
word = URLEncoder.encode(word, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
StringBuffer buffer = new StringBuffer("https://translate.google.cn/translate_a/single?client=t");
buffer.append("&sl=" + from);
buffer.append("&tl=" + to);
buffer.append("&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=btn&kc=0");
buffer.append("&tk=" + _tk);
buffer.append("&q=" + word);
browser.setUrl(buffer.toString());
try{
String result = browser.executeGet();
JSONArray array = (JSONArray) JSONArray.parse(result);
JSONArray r_array = array.getJSONArray(0);
StringBuffer r_buffer = new StringBuffer();
for(int i = 0; i 0);
if(StringUtils.isNotBlank(_r)){
r_buffer.append(_r);
}
}
return r_buffer.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
/*GoogleApi googleApi = new GoogleApi();*/
GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128);
String result = googleApi.translate("Many applications within the enterprise domain ", "", "zh");
System.out.println(result);
}
}
var b = function (a, b) {
for (var d = 0; d 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" 0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
var tk = function (a,TKK) {
//console.log(a,TKK);
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + "." + (a ^ h)
}
jar 包下载:
jar 包调用
public static void main(String[] args) {
/*GoogleApi googleApi = new GoogleApi();*/
GoogleApi googleApi = new GoogleApi("122.224.227.202", 3128);
String result = googleApi.translate("Test", "", "zh");
System.out.println(result);
}
jar 包输出
测试
写在最后:
package org.trans;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import org.trans.util.HttpClientUtil;
public class Browser
{
public Proxy proxy;
public String url;
public String getUrl()
{
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public Proxy getProxy() {
return this.proxy;
}
public void setProxy(String ip, Integer port) {
this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port.intValue()));
}
public String executeGet()
throws Exception
{
String result;
if (this.proxy != null)
result = HttpClientUtil.doGetWithProxy(this.url, this.proxy);
else {
result = HttpClientUtil.doGet(this.url);
}
return result;
}
}