调用有道云翻译接口 翻译类
package com.yangchong.fanyi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Scanner;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Fanyi {
// 创建json解析对象
private static JsonParser jp = new JsonParser();
public static void main(String[] args) throws IOException {
System.out.println("请输入要翻译的文本:");
Scanner sc = new Scanner(System.in);
String nr = sc.next();
fanyi(nr);
}
public static void fanyi(String nr) throws IOException {
File file = new File("F:\\" + nr + ".json");
if (file.exists()) {
return;
}
String abc = nr.matches("^[\u4e00-\u9fa5]{0,}$") ? URLEncoder.encode(nr, "utf-8") : nr;
// 调用有道接口的字符串
String jk = "http://fanyi.youdao.com/openapi.do?" + "keyfrom=yangchong&key=520150590&"
+ "type=data&doctype=json&version=1.1&q=" + abc;
// 创建RUL对象
URL url = new URL(jk);
// 打开连接
URLConnection urlConn = url.openConnection();
// 通过字节流获取网络数据
try {
InputStream is = urlConn.getInputStream();
FileOutputStream fos = new FileOutputStream("F:\\" + nr + ".json");
byte[] b = new byte[1024];
int i;
while ((i = is.read(b)) != -1) {
fos.write(b, 0, i);
}
// 关闭资源
fos.close();
is.close();
} catch (ConnectException e) {
System.err.println("访问超时!!");
}
}
public static String parserChinese(String nr) throws FileNotFoundException, UnsupportedEncodingException {
FileInputStream fis = new FileInputStream("F:\\" + nr + ".json");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");// 使用字符流转码
// 获得要解析的数据
JsonObject jo = (JsonObject) jp.parse(isr);
StringBuffer sb = new StringBuffer();
// 单词
sb.append(jo.get("query").getAsString() + " ");
// 有道翻译
JsonArray ja = jo.get("translation").getAsJsonArray();
for (int i = 0; i < ja.size(); i++) {
sb.append(ja.get(i).getAsString());
}
sb.append("\n");
// 音标
JsonObject py = (JsonObject) jo.get("basic");
sb.append("拼音:" + py.get("phonetic").getAsString() + "\n");
// 翻译
JsonArray fy = py.get("explains").getAsJsonArray();
for (int i = 0; i < fy.size(); i++) {
sb.append(fy.get(i).getAsString() + "\n");
}
return sb.toString();
}
/**
* 英译中
*
* @param nr
* 翻译的内容
* @return
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static String parserEnglish(String nr) throws FileNotFoundException, UnsupportedEncodingException {
// 获取要解析的资源文件
// FileReader fr = new FileReader("F:\\"+nr+".json");
FileInputStream fis = new FileInputStream("F:\\" + nr + ".json");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");// 使用字符流转码
// 获得要解析的数据
JsonObject jo = (JsonObject) jp.parse(isr);
StringBuffer sb = new StringBuffer();
// 单词
sb.append(jo.get("query").getAsString() + " ");
// 有道翻译
JsonArray ja = jo.get("translation").getAsJsonArray();
for (int i = 0; i < ja.size(); i++) {
sb.append(ja.get(i).getAsString());
}
sb.append("\n");
// 音标
JsonObject yb = (JsonObject) jo.get("basic");
String ybStr = "英[" + yb.get("uk-phonetic").getAsString() + "] " + "美[" + yb.get("us-phonetic").getAsString()
+ "]\n";
sb.append(ybStr);
// 翻译
JsonArray fy = yb.get("explains").getAsJsonArray();
for (int i = 0; i < fy.size(); i++) {
sb.append(fy.get(i).getAsString() + "\n");
}
return sb.toString();
}
}