我们上一篇初步体验了一把百度翻译api的魅力,由于分享作者是一位java程序员,所以下载的是百度翻译java语言开发包,这节主要分享介绍基于java web项目嵌入百度翻译api做个简单的中文翻译英文的示例。
1,打开Eclipse,新建一个web项目。
2,把从百度翻译开放平台下载的java开发包复制到web项目中,具体操作如下
1),jar包复制到web项目 lib目录下
2),把BaiduTranslateDemo.java文件复制到工程里
其中把此类中的静态变量更改为自己在百度翻译开放平台申请的参数值。
3),把resource文件下的baidu.xml文件复制到工程src下,其中修改此xml中BaiduTranslateDemo.java 的路径
3,新建java servlet为 BaiduFanyiServlet
package com.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.test.fanyi.BaiduTranslateDemo; /*** * * @author V型知识库 www.vxzsk.com * */ public class BaiduFanyiServlet extends HttpServlet { /** * Constructor of the object. */ public BaiduFanyiServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 将请求、响应的编码均设置为UTF-8(防止中文乱码) request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String source = request.getParameter("source"); String result; try { result = BaiduTranslateDemo.translateToEn(source); if(result == null){ System.out.println("翻译出错,参考百度错误代码和说明。"); return; } request.setAttribute("source", source); request.setAttribute("result", result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } request.getRequestDispatcher("/baidu/fanyi.jsp").forward(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
说明:doPost方法中调用百度翻译api,把从界面接收到的中文翻译成英文
4,在工程baidu文件夹下新建fanyi.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>百度翻译开放平台</title> </head> <body> <div align="center" style="margin-top: 100px"> <h4>百度翻译API开发示例</h4> <form action="/weChat/servlet/baiduFanyiServlet" method="post"> <table > <tr> <td>中文:<textarea name="source" style="width: 250px;height: 150px">${source }</textarea></td> <td>英文:<textarea style="width: 250px;height: 150px">${result }</textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="翻译成英文"></td> </tr> </table> </form> </div> </body> </html>
运行项目,可能出现找不到build.xml的异常信息,可在BaiduTranslateDemo.java的translateToEn方法配置build.xml文件的路径地址。
效果如下
此文章本站原创,地址 https://www.vxzsk.com/187.html
转载请注明出处!谢谢!
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程