import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController("/demo")
public class TranslateDemo{

    @PostMapping("123")
    public static void main(String[] args) throws Exception{
        String appkey = "YOUR_APPKEY";
        String devkey = "YOUR_DEVKEY";
        String translateUrl = "http://api.hcicloud.com:8880/mt/translate";
        String transData = "你好";
        String property = "cn2uy";  // 翻译方向：中文翻译维吾尔语

        // 计算出session key
        Date currentTime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String requestDate = dateFormat.format(currentTime);
        String sessionKey = MD5(requestDate+devkey);

        // 创建连接
        URL url=new URL(translateUrl);
        HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");

        // 设置请求参数，均为Header参数
        httpConn.setRequestProperty("x-app-key", appkey);
        httpConn.setRequestProperty("x-request-date", requestDate);
        httpConn.setRequestProperty("x-result-format", "json");
        httpConn.setRequestProperty("x-sdk-version", "7.0");
        httpConn.setRequestProperty("x-session-key", sessionKey);
        httpConn.setRequestProperty("x-task-config", "capkey=mt.cloud.translate,property="+property);
        httpConn.setRequestProperty("x-udid", "101:1234567890");
        httpConn.setRequestProperty("Charset", "UTF-8");

        // 发起连接
        httpConn.connect();

        // 发送待翻译的内容
        PrintWriter printWriter = new PrintWriter(httpConn.getOutputStream());
        printWriter.print(transData);
        printWriter.flush();
        printWriter.close();

        //获得响应状态
        int resultCode=httpConn.getResponseCode();
        StringBuffer stringBuffer=new StringBuffer();
        BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"))  ;
        String readLine = responseReader.readLine();
        responseReader.close();
        System.out.println(readLine);
    }

    public static String MD5(String key) {
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        try {
            byte[] btInput = key.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
}
