• 使用阿里云智能翻译接口案例——CSDN博客


    /**
         * 
        * @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();
    	    }
    	}

  • 相关阅读:
    【20171104中】chrome自动刷新网页
    【20171104早】python爬虫之username,password登陆
    【20171103中】sqli-libs Less 40-49
    .net orm类库 kiss.data 简单文档记录
    elasticsearch文档-字段的mapping
    elasticsearch文档-analysis
    elasticsearch文档-modules
    使用logstash+elasticsearch+kibana快速搭建日志平台
    Go语言Web框架gwk介绍 (五)
    Go语言Web框架gwk介绍 (四)
  • 原文地址:https://www.cnblogs.com/a1111/p/7459632.html
Copyright © 2020-2023  润新知