JAVA (J)ÀûÓÃGoogleµÄTralate¿ª·¢API_JSP¼¼ÇÉ_ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØ--Powered by WWW.RONGSEN.COM.CN

JAVA (J)ÀûÓÃGoogleµÄTralate¿ª·¢API

×÷ÕߣººÚ¿Í·ÀÏßÍø°²JSP½Ì³Ì»ùµØ À´Ô´£ººÚ¿Í·ÀÏßÍø°²JSP½Ì³Ì»ùµØ ä¯ÀÀ´ÎÊý£º0

±¾Æª¹Ø¼ü´Ê£º¿ª·¢ÀûÓàString
ºÚ¿Í·ÀÏßÍø°²ÍøѶ£ºÉÏ´ÎÎÒ˵ÁËÓÃASP,PHP,C#·Ö±ðʵÏÖGoogleµÄTranslate¿ª·¢Ò»¸öAPI £¨ Tags£ºGoogle Translate API £© £¬ÓÉÓںܶà³ÌÐò´úÂëÓÉJAVAʵÏÖ£¨JSP£©£¬ÎÒ»¹ÊÇÀûÓÃÒ»µãʱ¼ä×öÁËÒ»¸öjava°æ±¾µÄ£¬±¾ÈËÔÚJAV...

ÉÏ´ÎÎÒ˵ÁËÓÃASP,PHP,C#·Ö±ðʵÏÖGoogleµÄTranslate¿ª·¢Ò»¸öAPI £¨ Tags£ºGoogle Translate API £© £¬ÓÉÓںܶà³ÌÐò´úÂëÓÉJAVAʵÏÖ£¨JSP£©£¬ÎÒ»¹ÊÇÀûÓÃÒ»µãʱ¼ä×öÁËÒ»¸öjava°æ±¾µÄ£¬±¾ÈËÔÚJAVA·½Ã滹ÊÇÈëÃŵij̶ȣ¬Æڼ䣬²éÁËһЩ×ÊÁÏ£¬ËäÈ»³ÌÐò±È½Ï¶Ì£¬µ«ÊÇ»¹ÊDZȽÏÌÔÆøµÄ¡£

  ºÍÉÏÒ»»°Ëù˵µÄÀûÓÃJAVA»ñÈ¡URLµÄÄÚÈÝ£¬ÕâƪµÄ¼¼ÊõʵÏÖ»ñÈ¡£¬È»ºóÕýÔòÆ¥Åä½á¹û£¬¾ÍÍê³ÉÁË¡£¿´´úÂ룺

//javac GoogleTranslator.java -encoding utf-8
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.regex.*;
public class GoogleTranslator{
public String getUrlContent(String path){
        String rtn = "";
        int c;  
        try{
            java.net.URL   l_url   =   new   java.net.URL(path);  
            java.net.HttpURLConnection   l_connection   =   (java.net.HttpURLConnection)   l_url.openConnection();  
            l_connection.setRequestProperty("User-agent","Mozilla/4.0");
            l_connection.connect();  
            InputStream l_urlStream   =   l_connection.getInputStream();
            while   ((   (   c=   l_urlStream.read()   )!=-1)){ 
                int   all=l_urlStream.available();  
                byte[]   b   =new   byte[all];  
                l_urlStream.read(b);
                rtn+=   new   String(b,   "UTF-8");
            }  
                //Thread.sleep(2000);
            l_urlStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return rtn;
}
http://bizhi.knowsky.com
public String GetText(String Src){
    String Os=null;
    try{
        String pUrl="http://translate.google.com/translate_t?langpair="+URLEncoder.encode("zh-CN|en","utf-8")+"&text="+URLEncoder.encode(Src,"gb2312") ;
        String pageContent =getUrlContent(pUrl);
        if(!isNullOrEmpty(pageContent)){
            Os= GetMatchString(pageContent,"(<div id=result_box dir=\"ltr\">)([?:\\s\\S]*?)</div>",2);
        }
       
    }catch(Exception e){
            e.printStackTrace();
    }
    return Os;
}
private boolean isNullOrEmpty(String param) {
  return param == null || param.trim().length() == 0;
 }
public String GetMatchString(String text,String pattern,int point){
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(text);
    if(m.find()) {
        return m.group(point); //Æ¥ÅäµÚ1Ïî
    }
    return null;
}
public static void main(String[] args){
    GoogleTranslator obj=new GoogleTranslator();
    System.out.println(obj.GetText("cjjerÊÇJavaÌì²Å"));
}
};

È»ºó±£´æΪGoogleTranslator.java£¬È»ºóʹÓãº

javac GoogleTranslator.java -encoding utf-8

±àÒëÁË£¬È»ºóJAVAһϾͿÉÒÔÁË¡£

    ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤·½°¸±¾ÆªÁ¬½Ó£ºhttp://www.rongsen.com.cn/show-16657-1.html
Íøվά»¤½Ì³Ì¸üÐÂʱ¼ä:2012-04-07 00:44:31  ¡¾´òÓ¡´ËÒ³¡¿  ¡¾¹Ø±Õ¡¿
ÎÒÒªÉêÇë±¾Õ¾£ºNµã | ºÚ¿Í·ÀÏß¹ÙÍø |  
רҵ·þÎñÆ÷ά»¤¼°Íøվά»¤ÊÖ¹¤°²È«´î½¨»·¾³£¬ÍøÕ¾°²È«¼Ó¹Ì·þÎñ¡£ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØÕÐÉ̽øÐÐÖУ¡QQ:29769479

footer  footer  footer  footer