/** * * @Title: getTranslates * @Description: 该方法的主要作用:智能翻译 * @param 设定文件 * @return 返回类型:void * @throws */ public void getTranslates(){ String host = "https://dm-11.data.aliyun.com"; String path = "/rest/160601/mt/translate.json"; String method = "POST"; Map<String, String> headers = new HashMap<String, String>(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); //根据API的要求,定义相对应的Content-Type headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); Map<String, String> querys = new HashMap<String, String>(); Map<String, String> bodys = new HashMap<String, String>(); bodys.put("format", "text"); bodys.put("q", translate); String lauages[] = substring.getSubString(lauage); String lau1 = lauages[0]; //源语言 String lau2 = lauages[1]; //目标语言 bodys.put("source", lau1); bodys.put("target", lau2); try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); //获取response的body String result = EntityUtils.toString(response.getEntity()); JSONObject json = JSONObject.fromObject(result); String result1 = json.getString("data"); JSONObject json1 = JSONObject.fromObject(result1); String data = json1.getString("translatedText"); //翻译的结果 HttpServletResponse response1 = ServletActionContext.getResponse(); response1.setContentType("text/html;charset=utf-8"); PrintWriter out = response1.getWriter(); out.print(data); System.out.println(data); } catch (Exception e) { e.printStackTrace(); } }