zoukankan      html  css  js  c++  java
  • 使用阿里云智能翻译接口案例——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();
    	    }
    	}

  • 相关阅读:
    KindEditor编辑器的使用
    导航栏
    ajax php 验证注册用户名是否存在
    PS照片改底色
    截取邮箱@后面内容的两种情况
    js导航栏单击事件背景颜色变换
    model中的自动验证 $_validate $_auto
    一对一关联模型,HAS_ONE
    一对多关联模型,BELONGS_TO
    C++操作MySQL数据库
  • 原文地址:https://www.cnblogs.com/a1111/p/7459632.html
Copyright © 2011-2022 走看看