google在线翻译程序之java版

package qichao.Translate;

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class EnglishtoChina {

 public String etoc(String content){
        String url = "/translate_t";
        PostMethod postMethod = new UTF8PostMethod(url);
        //填入各个表单域的值
        NameValuePair[] data = {
                new NameValuePair("sl", "en"),
                new NameValuePair("tl", "zh-CN"),
                new NameValuePair("text", content),
                new NameValuePair("ie", "ISO-8859-1")
        };
        //将表单的值放入postMethod中
        postMethod.setRequestBody(data);
        //执行postMethod
        HttpClient httpClient = new HttpClient();
        httpClient.setConnectionTimeout(5000);
        httpClient.getHostConfiguration().setHost("translate.google.com", 80, "http");
        try {
   int state = httpClient.executeMethod(postMethod);
   if(state == 200){
             //打印结果页面
    content = postMethod.getResponseBodyAsString();
    content = parser(content);
   }
  }catch(HttpException e){
   e.printStackTrace();
  }catch(IOException e){
   e.printStackTrace();
  }
  return content;
 }
 public String parser(String content){
  Parser parser;
  try {
   parser = new Parser(content);
   NodeList listdiv = parser.parse(
     new AndFilter(
       new TagNameFilter("div"),new HasAttributeFilter("id","result_box")
     )
   );
   if(listdiv.size()>0){
    Node node = listdiv.elementAt(0);
    return node.toHtml();
   }
  } catch (ParserException e) {
   e.printStackTrace();
  }
  return null;
 }
 public static void main(String[] args) {
  EnglishtoChina s = new EnglishtoChina();
  String content = "National Seed Policy (NSP)  Quality seed is considered to be the basic input for increasing agricultural output and thereby achieving self-sufficiency in food production. Effectiveness of other inputs like fertilizer and irrigation depends largely on good seed. But use of improved seed is still very limited. Two major reasons behind this fact are: - Production and distribution of quality seed is insufficient in the public sector as compared to its demand;  - Seed production in the private sector has not yet got the necessary support.  Development of facilities in public and private sectors for production of sufficient quantity of improved seed and for making them available to the farmers at appropriate time and at reasonable price has been suffering from lack of definite policy directives. At the same time potentiality of technical assistance could not be explored due to absence of a clear Govt. policy in this field. With a view to overcoming this critical situation the MOA has formulated a National Seed Policy for the country.  A committee, formed by the MOA, reviewed the seed policies of a number of neighbouring countries and drafted a National Seed Policy drawing lessons and inputs from the experiences of countries having similar agro-ecological and socio-economic settings.  National Seed Policy provides for policy directives to increase production of improved seed both in the public and private sectors and for making best quality seeds available to the farmers on timely basis, and at competitive price. The seed policy has also provisions, among other things, for liberalisation of import of seed and seed processing machineries, strengthening of quality control and research system and maintaining a seed security arrangement. A major thrust of the seed policy has been on the institutional arrangement of the seed sector.  Select your language from below:  Open as pdf (84 KB) - English  Open as pdf (378 KB) - Bangla";
  content = s.etoc(content);
  System.out.println(content);
 }

}
class UTF8PostMethod extends PostMethod{
    public UTF8PostMethod(String url){
        super(url);
    }
    @Override
    public String getRequestCharSet() {
        //return super.getRequestCharSet();
        return "gb2312";
    }
}

 

 

 

 

 

 

你可能感兴趣的:(java,apache,IE,Security,Google)