PageRenderTime 1504ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/wordfix/util/TransUtil.java

https://gitlab.com/scottqq/wordfix
Java | 115 lines | 98 code | 16 blank | 1 comment | 4 complexity | d224c3a1686ef88be3c6f959f8f626fc MD5 | raw file
  1. package com.wordfix.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONObject;
  11. public class TransUtil
  12. {
  13. public static void main(String[] args)
  14. {
  15. System.out.println(translate("hello"));
  16. }
  17. public static String simpleTrans(String input)
  18. {
  19. try
  20. {
  21. JSONObject obj = JSON.parseObject(translate(input));
  22. return obj.getString("translation") + "<br/>" + obj.getJSONObject("basic").getString("explains");
  23. }
  24. catch(Exception e)
  25. {
  26. e.printStackTrace();
  27. }
  28. return "";
  29. }
  30. public static String translate(String input)
  31. {
  32. String ret = "";
  33. try
  34. {
  35. String strURL = " http://fanyi.youdao.com/openapi.do?keyfrom=kargocard&key=1719624492&type=data&doctype=json&version=1.1&q=" + URLEncoder.encode(input, "utf8");;
  36. ret = TransUtil.GetMsg(strURL);
  37. } catch (IOException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. return ret;
  42. }
  43. public static String PostMsg(String POST_URL, String strCotent) throws IOException
  44. {
  45. URL postUrl = new URL(POST_URL);
  46. HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
  47. System.setProperty("sun.net.client.defaultConnectTimeout", "3000");
  48. System.setProperty("sun.net.client.defaultReadTimeout", "3000");
  49. connection.setConnectTimeout(3000);
  50. connection.setReadTimeout(3000);
  51. connection.setDoOutput(true);
  52. connection.setDoInput(true);
  53. connection.setRequestMethod("POST");
  54. connection.setUseCaches(false);
  55. connection.setInstanceFollowRedirects(true);
  56. connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
  57. connection.connect();
  58. OutputStream out = connection.getOutputStream();
  59. out.write(strCotent.getBytes("utf-8"));
  60. out.close();
  61. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
  62. String line="";
  63. StringBuffer strBuf=new StringBuffer();
  64. System.out.println("Post URL="+POST_URL);
  65. while ((line = reader.readLine()) != null){
  66. strBuf.append(line);
  67. }
  68. reader.close();
  69. System.out.println("responseCode=>" + connection.getResponseCode() + "| responseMessage=>" + connection.getResponseMessage());
  70. connection.disconnect();
  71. return strBuf.toString();
  72. }
  73. public static String GetMsg(String POST_URL) throws IOException{
  74. URL postUrl = new URL(POST_URL);
  75. HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
  76. System.setProperty("sun.net.client.defaultConnectTimeout", "3000");
  77. System.setProperty("sun.net.client.defaultReadTimeout", "3000");
  78. connection.setConnectTimeout(3000);
  79. connection.setReadTimeout(3000);
  80. connection.setDoOutput(true);
  81. connection.setDoInput(true);
  82. connection.setRequestMethod("GET");
  83. connection.setUseCaches(false);
  84. connection.setInstanceFollowRedirects(true);
  85. connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
  86. connection.connect();
  87. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
  88. String line="";
  89. StringBuffer strBuf=new StringBuffer();
  90. System.out.println("Post URL="+POST_URL);
  91. while ((line = reader.readLine()) != null){
  92. strBuf.append(line);
  93. }
  94. System.out.println("responseCode=>" + connection.getResponseCode() + "| responseMessage=>" + connection.getResponseMessage());
  95. reader.close();
  96. connection.disconnect();
  97. return strBuf.toString();
  98. }
  99. }