Ver código fonte

增加下载文件重定向接口

lirong 2 anos atrás
pai
commit
bf8ce4e82e

+ 10 - 0
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/RedirectApi.java

@@ -1,9 +1,14 @@
 package com.persagy.ibms.data.sdk.service.rest;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.ibms.data.sdk.util.ExportUtil;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletResponse;
+
 @RestController
 public class RedirectApi {
 
@@ -18,4 +23,9 @@ public class RedirectApi {
 		String result = RedirectUtil.redirect(param, true);
 		return result;
 	}
+
+	@PostMapping(path = { "/redirect_export", "/zkt-sdk/redirect_export" }, produces = "application/json;charset=UTF-8")
+	public void alarm_export_post(@RequestBody String param, HttpServletResponse response) throws Exception {
+		RedirectUtil.redirect_export(param,response);
+	}
 }

+ 53 - 0
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/RedirectUtil.java

@@ -15,6 +15,8 @@ import com.persagy.ibms.data.sdk.util.HttpClientUtil;
 
 import lombok.extern.slf4j.Slf4j;
 
+import javax.servlet.http.HttpServletResponse;
+
 @Slf4j
 public class RedirectUtil {
 	public static String redirect(String param, boolean wrapper) {
@@ -111,4 +113,55 @@ public class RedirectUtil {
 		}
 		return result;
 	}
+
+	public static void redirect_export(String param,HttpServletResponse response) {
+		JSONObject sql_json = JSON.parseObject(param);
+		String code = sql_json.getString("code");
+		ConfigRedirect ConfigRedirect = Constant.ConfigRedirectMap.get(code);
+		try {
+			Map<String, String> otherMap = new ConcurrentHashMap<String, String>();
+			RedirectUtil.redirect_export_Inner(ConfigRedirect, sql_json, otherMap,response);
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+		}
+	}
+
+	public static void redirect_export_Inner(ConfigRedirect ConfigRedirect, JSONObject sql_json, Map<String, String> otherMap, HttpServletResponse response) throws Exception {
+		String url = ConfigRedirect.url;
+		String url_ori = url;
+		JSONObject urlParams = sql_json.getJSONObject("urlParams");
+		if (urlParams != null) {
+			StringBuffer sb = new StringBuffer();
+			StringBuffer sb_ori = new StringBuffer();
+			for (String key : urlParams.keySet()) {
+				if (sb.length() > 0) {
+					sb.append("&");
+					sb_ori.append("&");
+				}
+				sb.append(key + "=" + URLEncoder.encode(urlParams.get(key).toString(), "UTF-8"));
+				sb_ori.append(key + "=" + urlParams.get(key));
+			}
+			if (sb.length() > 0) {
+				url = url + "?" + sb.toString();
+				url_ori = url_ori + "?" + sb_ori.toString();
+			}
+		}
+		String urlSuffix = sql_json.getString("urlSuffix");
+		if (urlSuffix != null) {
+			url = url + urlSuffix;
+			url_ori = url_ori + "?" + urlSuffix;
+		}
+		otherMap.put("Url", url_ori);
+		log.warn(url_ori);
+		Object postBody = sql_json.get("postBody");
+		String postString;
+		if (postBody instanceof JSONObject) {
+			postString = JSONObject.toJSONString((JSONObject) postBody, SerializerFeature.WriteMapNullValue);
+		} else if (postBody instanceof JSONArray) {
+			postString = JSONArray.toJSONString((JSONArray) postBody, SerializerFeature.WriteMapNullValue);
+		} else {
+			postString = postBody.toString();
+		}
+		HttpClientUtil.instance("redirect").post_export(url, postString, null, ConfigRedirect.headerMap,response);
+	}
 }

+ 1 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/Constant.java

@@ -96,7 +96,7 @@ public class Constant {
 			suffixMap.put("-" + "meter-funcid", true);
 		}
 
-		try (InputStream stream = getStream("config_zs.xml")) {
+		try (InputStream stream = getStream("config.xml")) {
 			SAXReader saxReader = new SAXReader();
 			Document document = saxReader.read(stream);
 			if (document != null) {

+ 46 - 0
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/HttpClientUtil.java

@@ -2,6 +2,9 @@ package com.persagy.ibms.data.sdk.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -19,6 +22,9 @@ import org.apache.http.util.EntityUtils;
 import cn.hutool.core.lang.Tuple;
 import sun.misc.BASE64Encoder;
 
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+
 @SuppressWarnings("restriction")
 public class HttpClientUtil {
 	private static Map<String, HttpClientUtil> instanceMap = new ConcurrentHashMap<String, HttpClientUtil>();
@@ -208,4 +214,44 @@ public class HttpClientUtil {
 		Tuple tuple = new Tuple(guid, imageBase64);
 		return tuple;
 	}
+
+	public synchronized void post_export(String url, String content, Integer timeout, Map<String, String> headerMap, HttpServletResponse response) throws Exception {
+		if (Constant.http_interval > 0) {
+			Thread.sleep(Constant.http_interval);
+		}
+
+		HttpPost httpost = new HttpPost(url);
+		if (headerMap != null) {
+			for (String key : headerMap.keySet()) {
+				httpost.setHeader(key, headerMap.get(key));
+			}
+		}
+		if (timeout == null) {
+			timeout = 300000;
+		}
+		if (timeout != null) {
+			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+					.setSocketTimeout(timeout).build();
+			httpost.setConfig(requestConfig);
+		}
+		StringEntity entity = new StringEntity(content, "UTF-8");
+		httpost.setEntity(entity);
+		entity.setContentType("application/json");
+		HttpResponse httpResponse = client.execute(httpost);
+		InputStream is = httpResponse.getEntity().getContent();
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss.SSS");
+		String xlsFileName = sdf.format(new Date()) + ".xlsx";
+		xlsFileName = URLEncoder.encode(xlsFileName, "UTF-8");
+		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+		response.setHeader("Content-Disposition", "attachment;filename=" + xlsFileName);
+		ServletOutputStream out = response.getOutputStream();
+		// 输出
+		int len = 1;
+		byte[] bytes = new byte[1024];
+		while ((len = is.read(bytes)) != -1) {
+			out.write(bytes, 0, len);
+		}
+		out.close();
+		is.close();
+	}
 }