有道翻译API

以下是英语转汉语一个main方法



import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * desc:  
* date: 2019/07/18 * * @author wzf */ public class FanyiV3DemoInternalTest { private static Logger logger = LoggerFactory.getLogger(FanyiV3DemoInternalTest.class); private static final String YOUDAO_URL = "http://openapi.youdao.com/api"; private static final String APP_KEY = "YOUR_APP_KEY"; private static final String APP_SECRET = "YOUR_APP_SECRET"; public static void main(String[] args) throws IOException { Map params = new HashMap(); String q = "test"; String salt = String.valueOf(System.currentTimeMillis()); params.put("from", "EN"); params.put("to", "zh-CHS"); params.put("signType", "v3"); String curtime = String.valueOf(System.currentTimeMillis() / 1000); params.put("curtime", curtime); String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET; String sign = getDigest(signStr); params.put("appKey", APP_KEY); params.put("q", q); params.put("salt", salt); params.put("sign", sign); /** 处理结果 */ requestForHttp(YOUDAO_URL,params); } public static void requestForHttp(String url,Map params) throws IOException { /** 创建HttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /** httpPost */ HttpPost httpPost = new HttpPost(url); List paramsList = new ArrayList(); Iterator> it = params.entrySet().iterator(); while(it.hasNext()){ Map.Entry en = it.next(); String key = en.getKey(); String value = en.getValue(); paramsList.add(new BasicNameValuePair(key,value)); } httpPost.setEntity(new UrlEncodedFormEntity(paramsList,"UTF-8")); System.out.println("url:"+url); paramsList.forEach(val -> { System.out.println("name:"+val.getName()+",value:"+val.getValue()); }); /*CloseableHttpResponse httpResponse = httpClient.execute(httpPost); try{ Header[] contentType = httpResponse.getHeaders("Content-Type"); logger.info("Content-Type:" + contentType[0].getValue()); HttpEntity httpEntity = httpResponse.getEntity(); String json = EntityUtils.toString(httpEntity,"UTF-8"); EntityUtils.consume(httpEntity); logger.info(json); System.out.println(json); }finally { try{ if(httpResponse!=null){ httpResponse.close(); } }catch(IOException e){ logger.info("## release resouce error ##" + e); } }*/ } /** * 生成加密字段 */ public static String getDigest(String string) { if (string == null) { return null; } char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; byte[] btInput = string.getBytes(); try { MessageDigest mdInst = MessageDigest.getInstance("SHA-256"); mdInst.update(btInput); byte[] md = mdInst.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (byte byte0 : md) { str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (NoSuchAlgorithmException e) { return null; } } /** * * @param result 音频字节流 * @param file 存储路径 */ private static void byte2File(byte[] result, String file) { File audioFile = new File(file); FileOutputStream fos = null; try{ fos = new FileOutputStream(audioFile); fos.write(result); }catch (Exception e){ logger.info(e.toString()); }finally { if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String truncate(String q) { if (q == null) { return null; } int len = q.length(); String result; return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len)); } }

 

你可能感兴趣的:(java)