|
@@ -0,0 +1,312 @@
|
|
|
+package com.onemap.api.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.onemap.common.core.utils.StringUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.methods.HttpUriRequest;
|
|
|
+import org.apache.http.client.methods.RequestBuilder;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.entity.mime.content.StringBody;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.channels.FileChannel;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 调用厅里接口工具类
|
|
|
+ */
|
|
|
+public class RInterfaceUtil {
|
|
|
+ public static String Authorization = "Basic c2FiZXI6c2FiZXJfc2VjcmV0";
|
|
|
+ public static String TenantId = "000000";
|
|
|
+
|
|
|
+ public static long sliceSize = 10 * 1024 * 1024;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用远程接口
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param jsonParam access_token、param都为空时,为获取token请求
|
|
|
+ * access_token不为空 param为空时,为gfindByMd5Url请求
|
|
|
+ * access_token、param都不为空时,为上传文件请求
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject postRemote(String access_token, String url, JSONObject jsonParam, Map<String, Object> param) {
|
|
|
+ //CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
|
|
+ RequestConfig config = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(0)
|
|
|
+ .setSocketTimeout(0)
|
|
|
+ .setConnectionRequestTimeout(0)
|
|
|
+ .build();
|
|
|
+ CloseableHttpClient closeableHttpClient = HttpClients.custom()
|
|
|
+ .setDefaultRequestConfig(config)
|
|
|
+ .build();
|
|
|
+ CloseableHttpResponse closeableHttpResponse = null;
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ try {
|
|
|
+ if (param != null) {
|
|
|
+ //文件上传
|
|
|
+ ContentType ctype = ContentType.create("content-disposition", "UTF-8");
|
|
|
+ MultipartEntityBuilder mentity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
|
|
|
+ Set<String> keyset = param.keySet();
|
|
|
+ for (String key : keyset) {
|
|
|
+ System.out.println("postRemote 文件上传参数:" + key + ":" + param.get(key));
|
|
|
+ Object paramObj = param.get(key);
|
|
|
+ if (paramObj != null) {
|
|
|
+ if (paramObj instanceof MultipartFile) {
|
|
|
+ mentity.addBinaryBody(key, ((MultipartFile) paramObj).getInputStream(), ctype, ((MultipartFile) paramObj).getOriginalFilename());
|
|
|
+ } else if (paramObj instanceof File) {
|
|
|
+ mentity.addBinaryBody(key, (File) paramObj);//(key, new FileInputStream((File)paramObj),ctype,((File)paramObj).getName());
|
|
|
+ } else {
|
|
|
+ mentity.addPart(key, new StringBody(paramObj.toString(), ctype));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpEntity entity = mentity.build();
|
|
|
+ HttpUriRequest post = RequestBuilder.post().setUri(url).setEntity(entity).build();
|
|
|
+ //设置HTTP访问header
|
|
|
+ post.setHeader("Authorization", Authorization);
|
|
|
+ post.addHeader("Tenant-Id", TenantId);
|
|
|
+ if (StringUtils.isNotEmpty(access_token)) {
|
|
|
+ post.setHeader("Token-Auth", access_token);
|
|
|
+ }
|
|
|
+ closeableHttpResponse = closeableHttpClient.execute(post);
|
|
|
+ } else {
|
|
|
+ //普通请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ //设置HTTP访问header
|
|
|
+ httpPost.setHeader("Authorization", Authorization);
|
|
|
+ httpPost.addHeader("Tenant-Id", TenantId);
|
|
|
+ if (StringUtils.isNotEmpty(access_token)) {
|
|
|
+ httpPost.setHeader("Token-Auth", access_token);
|
|
|
+ }
|
|
|
+ //设置请求参数
|
|
|
+ //StringEntity entity = new StringEntity(jsonParam.toString(), ContentType.create("text/json", "UTF-8"));
|
|
|
+ List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
|
|
|
+ for (String str : jsonParam.keySet()) {
|
|
|
+ list.add(new BasicNameValuePair(str, jsonParam.getString(str)));
|
|
|
+ System.out.println(" key : str : " + str + " value : " + jsonParam.getString(str));
|
|
|
+ }
|
|
|
+ //创建参数集合
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ //配置请求时间、超时时间
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ //开始发送请求
|
|
|
+ closeableHttpResponse = closeableHttpClient.execute(httpPost);
|
|
|
+ }
|
|
|
+ //获取请求状态码
|
|
|
+ int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
|
+ //请求状态码放入到返回json
|
|
|
+ jsonObject.put("code", statusCode);
|
|
|
+ if (statusCode != HttpStatus.SC_OK) {
|
|
|
+ //TODO:状态码非200代表没有正常返回,此处处理你的业务
|
|
|
+ HttpEntity httpEntity = closeableHttpResponse.getEntity();
|
|
|
+ String asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
+ try {
|
|
|
+ //错误是否能解析成json,不能的话直接返回字符串到msg
|
|
|
+ jsonObject = JSONObject.parseObject(asset_synchronization);
|
|
|
+ } catch (Exception e) {
|
|
|
+ jsonObject.put("msg", asset_synchronization);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //返回值200
|
|
|
+ HttpEntity httpEntity = closeableHttpResponse.getEntity();
|
|
|
+ String asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
+ return JSONObject.parseObject(asset_synchronization);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ //程序异常返回值
|
|
|
+ jsonObject.put("code", -1);
|
|
|
+ jsonObject.put("msg", e.toString());
|
|
|
+ return jsonObject;
|
|
|
+ } finally {
|
|
|
+ if (closeableHttpResponse != null) {
|
|
|
+ try {
|
|
|
+ //关闭http请求
|
|
|
+ closeableHttpResponse.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("postRemote jsonObject: " + jsonObject);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用远程文件流接口
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param jsonParam access_token、param都为空时,为获取token请求
|
|
|
+ * access_token不为空 param为空时,为gfindByMd5Url请求
|
|
|
+ * access_token、param都不为空时,为上传文件请求
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject postRemoteFile(String access_token, String url, JSONObject jsonParam, Map<String, Object> param, HttpServletResponse response) {
|
|
|
+ CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
|
|
+ CloseableHttpResponse closeableHttpResponse = null;
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ try {
|
|
|
+ if (param != null) {
|
|
|
+ //文件上传
|
|
|
+ ContentType ctype = ContentType.create("content-disposition", "UTF-8");
|
|
|
+ MultipartEntityBuilder mentity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
|
|
|
+ Set<String> keyset = param.keySet();
|
|
|
+ for (String key : keyset) {
|
|
|
+ System.out.println("postRemote 文件上传参数:" + key + ":" + param.get(key));
|
|
|
+ Object paramObj = param.get(key);
|
|
|
+ if (paramObj != null) {
|
|
|
+ if (paramObj instanceof MultipartFile) {
|
|
|
+ mentity.addBinaryBody(key, ((MultipartFile) paramObj).getInputStream(), ctype, ((MultipartFile) paramObj).getOriginalFilename());
|
|
|
+ } else if (paramObj instanceof File) {
|
|
|
+ mentity.addBinaryBody(key, (File) paramObj);//(key, new FileInputStream((File)paramObj),ctype,((File)paramObj).getName());
|
|
|
+ } else {
|
|
|
+ mentity.addPart(key, new StringBody(paramObj.toString(), ctype));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpEntity entity = mentity.build();
|
|
|
+ HttpUriRequest post = RequestBuilder.post().setUri(url).setEntity(entity).build();
|
|
|
+ //设置HTTP访问header
|
|
|
+ post.setHeader("Authorization", Authorization);
|
|
|
+ post.addHeader("Tenant-Id", TenantId);
|
|
|
+ if (StringUtils.isNotEmpty(access_token)) {
|
|
|
+ post.setHeader("Token-Auth", access_token);
|
|
|
+ }
|
|
|
+ closeableHttpResponse = closeableHttpClient.execute(post);
|
|
|
+ } else {
|
|
|
+ //普通请求
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ //设置HTTP访问header
|
|
|
+ httpPost.setHeader("Authorization", Authorization);
|
|
|
+ httpPost.addHeader("Tenant-Id", TenantId);
|
|
|
+ if (StringUtils.isNotEmpty(access_token)) {
|
|
|
+ httpPost.setHeader("Token-Auth", access_token);
|
|
|
+ }
|
|
|
+ //设置请求参数
|
|
|
+ //StringEntity entity = new StringEntity(jsonParam.toString(), ContentType.create("text/json", "UTF-8"));
|
|
|
+ List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
|
|
|
+ for (String str : jsonParam.keySet()) {
|
|
|
+ list.add(new BasicNameValuePair(str, jsonParam.getString(str)));
|
|
|
+ System.out.println(" key : str : " + str + " value : " + jsonParam.getString(str));
|
|
|
+ }
|
|
|
+ //创建参数集合
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ //配置请求时间、超时时间
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ //开始发送请求
|
|
|
+ closeableHttpResponse = closeableHttpClient.execute(httpPost);
|
|
|
+ }
|
|
|
+ //获取请求状态码
|
|
|
+ int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
|
+ //请求状态码放入到返回json
|
|
|
+ jsonObject.put("code", statusCode);
|
|
|
+ if (statusCode != HttpStatus.SC_OK) {
|
|
|
+ //TODO:状态码非200代表没有正常返回,此处处理你的业务
|
|
|
+ HttpEntity httpEntity = closeableHttpResponse.getEntity();
|
|
|
+ String asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
+ try {
|
|
|
+ //错误是否能解析成json,不能的话直接返回字符串到msg
|
|
|
+ jsonObject = JSONObject.parseObject(asset_synchronization);
|
|
|
+ } catch (Exception e) {
|
|
|
+ jsonObject.put("msg", asset_synchronization);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //返回值200
|
|
|
+ HttpEntity httpEntity = closeableHttpResponse.getEntity();
|
|
|
+ String responseType = httpEntity.getContentType().getValue();
|
|
|
+ if (responseType.contains("zip")) {
|
|
|
+ byte[] outstream = EntityUtils.toByteArray(httpEntity);
|
|
|
+ response.getOutputStream().write(outstream);
|
|
|
+ response.getOutputStream().close();
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ String asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
+ return JSONObject.parseObject(asset_synchronization);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ //程序异常返回值
|
|
|
+ jsonObject.put("code", -1);
|
|
|
+ jsonObject.put("msg", e.toString());
|
|
|
+ return jsonObject;
|
|
|
+ } finally {
|
|
|
+ if (closeableHttpResponse != null) {
|
|
|
+ try {
|
|
|
+ //关闭http请求
|
|
|
+ closeableHttpResponse.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("postRemote jsonObject: " + jsonObject);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切割文件
|
|
|
+ *
|
|
|
+ * @param fromFileName 源文件的完整路径带文件名
|
|
|
+ * @param toFileName 目标文件的完整路径带文件名
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static long spilt(String fromFileName, String toFileName, String splice) throws IOException {
|
|
|
+ long curSliceSize = 0;
|
|
|
+ File f = new File(fromFileName);
|
|
|
+ FileInputStream in = new FileInputStream(f);
|
|
|
+ FileOutputStream out = null;
|
|
|
+ FileChannel inChannel = in.getChannel();
|
|
|
+ FileChannel outChannel = null;
|
|
|
+ // 计算最终会分成几个文件
|
|
|
+ int count = (int) (f.length() / sliceSize);
|
|
|
+ for (int i = 0; i <= count; i++) {
|
|
|
+ // 生成文件的路径
|
|
|
+ String t = toFileName + splice + i;
|
|
|
+ try {
|
|
|
+ out = new FileOutputStream(new File(t));
|
|
|
+ outChannel = out.getChannel();
|
|
|
+ // 从inChannel的m*i处,读取固定长度的数据,写入outChannel
|
|
|
+ if (i != count) {
|
|
|
+ curSliceSize = sliceSize;
|
|
|
+ inChannel.transferTo(sliceSize * i, curSliceSize, outChannel);
|
|
|
+ } else {// 最后一个文件,大小不固定,所以需要重新计算长度
|
|
|
+ curSliceSize = f.length() - sliceSize * count;
|
|
|
+ inChannel.transferTo(sliceSize * i, curSliceSize, outChannel);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.toString());
|
|
|
+ return curSliceSize;
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ outChannel.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ inChannel.close();
|
|
|
+ return curSliceSize;
|
|
|
+ }
|
|
|
+}
|