java谷歌翻译_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";     } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值