httpclient로 무료 구글 번역api 구현

Google translate API가 X를 받기 때문에 C를 훔치는 방법을 생각해 봤습니다.
1. HttpClientl로 req 보내기uestohttp://translate.google.com
2. Jsoupparse html, K로 g를 뒤집은 문자를 꺼내기

/**
 * Copyright (c) blackbear, Inc All Rights Reserved.
 */
package org.bb.util.i18n;

import java.io.InputStream;
import java.net.URLEncoder;
import java.text.MessageFormat;

import org.apache.commons.io.IOUtils;

import org.bb.util.net.http.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * TranslateUtil
 *
 * <pre> g
 * PS: ^google translate
 * </pre>
 *
 * @author catty
 * @version 1.0, Created on 2011/9/2
 */
public class TranslateUtil {

 protected static final String URL_TEMPLATE = "http://translate.google.com/?langpair={0}&text={1}";
 protected static final String ID_RESULTBOX = "result_box";
 protected static final String ENCODING = "UTF-8";

 protected static final String AUTO = "auto"; // google Z
 protected static final String TAIWAN = "zh-TW"; //
 protected static final String CHINA = "zh-CN"; //
 protected static final String ENGLISH = "en"; //
 protected static final String JAPAN = "ja"; //

 /**
  * <pre>Google g
  * PS: google Z
  * </pre>
  *
  * @param text
  * @param target_lang Z
  * @return
  * @throws Exception
  */
 public static String translate(final String text, final String target_lang) throws Exception {
  return translate(text, AUTO, target_lang);
 }

 /**
  * <pre>Google g</pre>
  *
  * @param text
  * @param src_lang Z
  * @param target_lang Z
  * @return
  * @throws Exception
  */
 public static String translate(final String text, final String src_lang, final String target_lang)
   throws Exception {
  InputStream is = null;
  Document doc = null;
  Element ele = null;
  try {
   // create URL string
   String url = MessageFormat.format(URL_TEMPLATE,
     URLEncoder.encode(src_lang + "|" + target_lang, ENCODING),
     URLEncoder.encode(text, ENCODING));

   // connect & download html
   is = HttpClientUtil.downloadAsStream(url);

   // parse html by Jsoup
   doc = Jsoup.parse(is, ENCODING, "");
   ele = doc.getElementById(ID_RESULTBOX);
   String result = ele.text();
   return result;

  } finally {
   IOUtils.closeQuietly(is);
   is = null;
   doc = null;
   ele = null;
  }
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String cn2tw(final String text) throws Exception {
  return translate(text, CHINA, TAIWAN);
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String tw2cn(final String text) throws Exception {
  return translate(text, TAIWAN, CHINA);
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String en2tw(final String text) throws Exception {
  return translate(text, ENGLISH, TAIWAN);
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String tw2en(final String text) throws Exception {
  return translate(text, TAIWAN, ENGLISH);
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String jp2tw(final String text) throws Exception {
  return translate(text, JAPAN, TAIWAN);
 }

 /**
  * <pre>Google g: --> </pre>
  *
  * @param text
  * @return
  * @throws Exception
  */
 public static String tw2jp(final String text) throws Exception {
  return translate(text, TAIWAN, JAPAN);
 }
}

HttpClientUtil.java

/**
 * Copyright (c) Blackbear, Inc All Rights Reserved.
 */
package org.bb.util.net.http;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;

/**
 * PostUtil.java
 *
 * @author catty
 * @version 1.0, Created on 2008/2/20
 */
public class HttpClientUtil {

 protected static Log log = LogFactory.getLog(HttpClientUtil.class);
 protected static HttpClient httpclient = null;
 protected static int maxTotal = 200;
 protected static int maxPerRoute = 20;
 protected static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7";

 static {
  if (httpclient == null) {
   // ~~~~~~~~~~~~~~~~~~~~
   // create httpclient
   // ~~~~~~~~~~~~~~~~~~~~
   SchemeRegistry reg = new SchemeRegistry();
   reg.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
   reg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
   ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(reg);
   cm.setMaxTotal(maxTotal);
   cm.setDefaultMaxPerRoute(maxPerRoute);
   httpclient = new DefaultHttpClient(cm);
  }
 }

 /**
  * <pre> d Inputstream</pre>
  *
  * @param url
  * @return
  * @throws Exception
  */
 public static InputStream downloadAsStream(String url) throws Exception {
  InputStream is = (InputStream) download(url, null, null, false);
  return is;
 }

 /**
  * <pre> d Υ File</pre>
  *
  * @param url
  * @param saveFile
  * @throws Exception
  */
 public static void download(String url, File saveFile) throws Exception {
  download(url, saveFile, null, false);
 }

 /**
  * <pre> d</pre>
  *
  * @param url
  * @param saveFile
  * @param params
  * @param isPost
  * @return saveFile==nullt inputstream, t saveFile
  * @throws Exception
  */
 public static Object download(final String url, final File saveFile, final Map<String, String> params,
   final boolean isPost) throws Exception {

  boolean saveToFile = saveFile != null;

  // check dir exist ??
  if (saveToFile && saveFile.getParentFile().exists() == false) {
   saveFile.getParentFile().mkdirs();
  }

  Exception err = null;
  HttpRequestBase request = null;
  HttpResponse response = null;
  HttpEntity entity = null;
  FileOutputStream fos = null;
  Object result = null;

  try {
   // create request
   if (isPost) {
    request = new HttpPost(url);
   } else {
    request = new HttpGet(url);
   }

   // add header & params
   addHeaderAndParams(request, params);

   // connect
   response = httpclient.execute(request);
   entity = response.getEntity();
   entity = new BufferedHttpEntity(entity);

   // get result
   if (saveToFile) {// save to disk
    fos = new FileOutputStream(saveFile);
    IOUtils.copy(entity.getContent(), fos);
    result = saveFile;
   } else { // warp to inpustream
    result = new BufferedInputStream(entity.getContent());
   }

  } catch (Exception e) {
   err = e;
  } finally {

   // close
   IOUtils.closeQuietly(fos);

   // clear
   request = null;
   response = null;
   entity = null;

   if (err != null) {
    throw err;
   }

   return result;
  }

 }

 protected static void addHeaderAndParams(final HttpRequestBase request, final Map<String, String> params) {
  // add default header
  request.addHeader("User-Agent", userAgent);

  // add params
  if (params != null) {

   // map --> HttpParams
   BasicHttpParams myParams = new BasicHttpParams();
   for (String key : params.keySet()) {
    myParams.setParameter(key, params.get(key));
   }

   request.setParams(myParams);
  }
 }

 public static HttpClient getHttpclient() {
  return httpclient;
 }

 public static void setHttpclient(HttpClient httpclient) {
  HttpClientUtil.httpclient = httpclient;
 }

 public static int getMaxTotal() {
  return maxTotal;
 }

 public static void setMaxTotal(int maxTotal) {
  HttpClientUtil.maxTotal = maxTotal;
 }

 public static int getMaxPerRoute() {
  return maxPerRoute;
 }

 public static void setMaxPerRoute(int maxPerRoute) {
  HttpClientUtil.maxPerRoute = maxPerRoute;
 }

 public static String getUserAgent() {
  return userAgent;
 }

 public static void setUserAgent(String userAgent) {
  HttpClientUtil.userAgent = userAgent;
 }
}

좋은 웹페이지 즐겨찾기