|
@@ -5,6 +5,8 @@ import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.net.HttpURLConnection;
|
|
@@ -19,7 +21,7 @@ import java.util.Map;
|
|
|
@Controller
|
|
|
@RequestMapping("translationInterface")
|
|
|
@CrossOrigin(origins = "*")
|
|
|
-public class Test {
|
|
|
+public class Translation {
|
|
|
/**
|
|
|
* 翻译接口
|
|
|
* @param translationVal 参数
|
|
@@ -27,11 +29,14 @@ public class Test {
|
|
|
*/
|
|
|
@RequestMapping("translation")
|
|
|
@ResponseBody
|
|
|
- public String translation(String translationVal, String from, String to){
|
|
|
+ public JSONObject translation(String translationVal, String from, String to, HttpServletRequest request){
|
|
|
// 请求url
|
|
|
String url = "https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1";
|
|
|
+ Map<String, Object> mapVal = new HashMap<>();
|
|
|
|
|
|
try {
|
|
|
+ String ip = request.getHeader("Host");
|
|
|
+ System.out.println(ip);
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("from", from);
|
|
|
map.put("to", to);
|
|
@@ -41,8 +46,11 @@ public class Test {
|
|
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
|
|
|
String accessToken = accessToken();
|
|
|
String result = HttpUtil.post(url, accessToken, "application/json", param);
|
|
|
- System.out.println(result);
|
|
|
- return result;
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+// JSONObject jsonObject1 = (JSONObject) jsonObject.get("result");
|
|
|
+ System.out.println(jsonObject);
|
|
|
+// List<Map<Object, Object>> list = (List<Map<Object, Object>>) jsonObject1.get("trans_result");
|
|
|
+ return jsonObject;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -87,7 +95,6 @@ public class Test {
|
|
|
* 返回结果示例
|
|
|
*/
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
- System.out.println(jsonObject);
|
|
|
String access_token = jsonObject.getString("access_token");
|
|
|
return access_token;
|
|
|
} catch (Exception e) {
|