|
@@ -0,0 +1,372 @@
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.util;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * http 工具类
|
|
|
+ */
|
|
|
+public class HttpUtil {
|
|
|
+
|
|
|
+ public static String post(String requestUrl, String accessToken, String params) throws Exception {
|
|
|
+ System.out.println(params);
|
|
|
+ String generalUrl = "";
|
|
|
+ generalUrl = requestUrl + "?access_token=" + accessToken;
|
|
|
+ System.out.println("发送的连接为:"+generalUrl);
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.writeBytes(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader in = null;
|
|
|
+ if (requestUrl.contains("nlp"))
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
|
|
|
+ else
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String result = "";
|
|
|
+ String getLine;
|
|
|
+ while ((getLine = in.readLine()) != null) {
|
|
|
+ result += getLine;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ System.out.println("请求结束"+new Date().getTime()/1000);
|
|
|
+ System.out.println("result:" + result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public static String postUnit(String requestUrl, String accessToken, String params) throws Exception {
|
|
|
+ String generalUrl = requestUrl + "?access_token=" + accessToken;
|
|
|
+ System.out.println("发送的连接为:"+generalUrl);
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
|
|
|
+ out.write(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader in = null;
|
|
|
+ if (requestUrl.contains("nlp"))
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
|
|
|
+ else
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String result = "";
|
|
|
+ String getLine;
|
|
|
+ while ((getLine = in.readLine()) != null) {
|
|
|
+ result += getLine;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ System.out.println("请求结束"+new Date().getTime()/1000);
|
|
|
+ System.out.println("result:" + result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 语音合成HTTP方法
|
|
|
+ * @param requestUrl 请求的接口地址 拼接access_token后的
|
|
|
+ * @param params 语音合成的参数
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String postVoice(String requestUrl,String params) throws Exception {
|
|
|
+ String workspace = System.getProperty("user.home");
|
|
|
+ String path = workspace+"/text2audio/";
|
|
|
+ try {
|
|
|
+ if (!(new File(path).isDirectory())) {
|
|
|
+ new File(path).mkdir();
|
|
|
+ }
|
|
|
+ } catch (SecurityException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String filePath = path+"VOICE"+new Date().getTime()/1000+".mp3";
|
|
|
+ String generalUrl = requestUrl;
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ System.out.println(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.writeBytes(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(filePath);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = -1;
|
|
|
+ while ((len=inputStream.read(buffer))!=-1) {
|
|
|
+ outputStream.write(buffer,0,len);
|
|
|
+ }
|
|
|
+ outputStream.close();
|
|
|
+ System.out.println("请求结束"+new Date().getTime()/1000);
|
|
|
+ System.out.println("MP3文件保存目录:" + filePath);
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取语音识别内容
|
|
|
+ * @param requestUrl
|
|
|
+ * @param params
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String postASR(String requestUrl, String params) throws Exception {
|
|
|
+ System.out.println(params);
|
|
|
+ String generalUrl = requestUrl;
|
|
|
+ System.out.println("发送的连接为:"+generalUrl);
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.writeBytes(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader in = null;
|
|
|
+ if (requestUrl.contains("nlp"))
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
|
|
|
+ else
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String result = "";
|
|
|
+ String getLine;
|
|
|
+ while ((getLine = in.readLine()) != null) {
|
|
|
+ result += getLine;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ System.out.println("请求结束"+new Date().getTime()/1000);
|
|
|
+ System.out.println("result:" + result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public static String postTest(String requestUrl,String params) throws Exception {
|
|
|
+ String workspace = System.getProperty("user.home");
|
|
|
+ String path = workspace+"/text2audio/";
|
|
|
+ try {
|
|
|
+ if (!(new File(path).isDirectory())) {
|
|
|
+ new File(path).mkdir();
|
|
|
+ }
|
|
|
+ } catch (SecurityException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String filePath = path+"VOICE"+new Date().getTime()/1000+".apk";
|
|
|
+ String generalUrl = requestUrl;
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ System.out.println("打开链接,开始发送请求"+new Date().getTime()/1000);
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.writeBytes(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(filePath);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = -1;
|
|
|
+ while ((len=inputStream.read(buffer))!=-1) {
|
|
|
+ outputStream.write(buffer,0,len);
|
|
|
+ }
|
|
|
+ outputStream.close();
|
|
|
+ System.out.println("请求结束"+new Date().getTime()/1000);
|
|
|
+ System.out.println("MP3文件保存目录:" + filePath);
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+ public static String post(String requestUrl,String params) throws Exception {
|
|
|
+ String generalUrl = requestUrl;
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.writeBytes(params);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader in = null;
|
|
|
+ if (requestUrl.contains("nlp"))
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ else
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String result = "";
|
|
|
+ String getLine;
|
|
|
+ while ((getLine = in.readLine()) != null) {
|
|
|
+ result += getLine;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ System.out.println("result:" + result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * NLP接口HTTP请求方法
|
|
|
+ * @param requestUrl
|
|
|
+ * @param params
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String postNLP(String requestUrl,String params) throws Exception {
|
|
|
+ String encoding = "";
|
|
|
+ if(requestUrl.contains("nlp")){
|
|
|
+ encoding = "GBK";
|
|
|
+ }else{
|
|
|
+ encoding = "UTF-8";
|
|
|
+ }
|
|
|
+ String generalUrl = requestUrl;
|
|
|
+ URL url = new URL(generalUrl);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // 得到请求的输出流对象
|
|
|
+ DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+ out.write(params.getBytes(encoding));
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 建立实际的连接
|
|
|
+ connection.connect();
|
|
|
+ // 获取所有响应头字段
|
|
|
+ Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+ // 遍历所有的响应头字段
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ System.out.println(key + "--->" + headers.get(key));
|
|
|
+ }
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader in = null;
|
|
|
+ if (requestUrl.contains("nlp_wordseg")||requestUrl.contains("nlp_wordpos")||requestUrl.contains("nlp_wordner")||requestUrl.contains("nlp_wordsyn"))
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
|
|
|
+ else
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String result = "";
|
|
|
+ String getLine;
|
|
|
+ while ((getLine = in.readLine()) != null) {
|
|
|
+ result += getLine;
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ System.out.println("result:" + result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|