Commit bd4eb468 by liulianglang

fj

parent fb442f17
......@@ -6,6 +6,7 @@ import org.springblade.founder.utils.HttpUtil;
import org.springblade.founder.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
......@@ -14,6 +15,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
......@@ -53,6 +55,7 @@ public class ForwardController {
System.out.println("url2:=="+url);
HashMap<String, String> headers = new HashMap<>();
Enumeration<String> headerNames = request.getHeaderNames();
String data="{}";
while (headerNames.hasMoreElements()) {
String element = headerNames.nextElement();
if ("blade-auth".equals(element) || "authorization".equals(element)) {
......@@ -60,6 +63,9 @@ public class ForwardController {
headers.put(element, header);
System.out.println("header:=="+element);
}
if("sjdata".equals(element)){
data = request.getHeader(element);
}
}
String ip;
try {
......@@ -73,15 +79,17 @@ public class ForwardController {
String contentType = request.getContentType();
if ("application/json".equals(contentType)) {
try {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
sb.append(inputStr);
}
String s = HttpUtil.doPostJson(host + url, headers, sb.toString());
// BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
// StringBuilder sb = new StringBuilder();
// String inputStr;
// while ((inputStr = streamReader.readLine()) != null) {
// sb.append(inputStr);
// }
data=URLDecoder.decode(data);
String s = HttpUtil.doPostJson(host + url, headers, data);
return JSONObject.parseObject(s, R.class);
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
return R.error(e.getMessage());
}
......@@ -94,7 +102,16 @@ public class ForwardController {
}
try {
System.out.println("postparamsr:=="+params);
String s = HttpUtil.doPost(host + url, params, headers);
// String str = "{\"age\":\"24\",\"name\":\"summer\"}";
System.out.println("datayss:=="+data);
data=URLDecoder.decode(data);
if("".equals(data)){
data="{}";
}
JSONObject jsonObject = JSONObject.parseObject(data);
Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString());
System.out.println("data:=="+jsonToMap);
String s = HttpUtil.doPost2(host + url, jsonToMap, headers);
return JSONObject.parseObject(s, R.class);
} catch (Exception e) {
e.printStackTrace();
......@@ -137,12 +154,17 @@ public class ForwardController {
System.out.println("url:=="+url);
HashMap<String, String> headers = new HashMap<>();
Enumeration<String> headerNames = request.getHeaderNames();
String data="{}";
while (headerNames.hasMoreElements()) {
String element = headerNames.nextElement();
if ("blade-auth".equals(element) || "authorization".equals(element)) {
String header = request.getHeader(element);
headers.put(element, header);
System.out.println("header:=="+element);
}
if("sjdata".equals(element)){
data = request.getHeader(element);
}
}
Map<String, String[]> map = request.getParameterMap();
......@@ -151,7 +173,18 @@ public class ForwardController {
String[] strings = map.get(key);
params.put(key, strings[0]);
}
String s = HttpUtil.uploadFile(host + url, file, file.getName(), headers, params);
System.out.println("postparamsr:=="+params);
data=URLDecoder.decode(data);
System.out.println("datayss:=="+data);
data=URLDecoder.decode(data);
if("".equals(data)){
data="{}";
}
// String str = "{\"age\":\"24\",\"name\":\"summer\"}";
JSONObject jsonObject = JSONObject.parseObject(data);
Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString());
System.out.println("data:=="+jsonToMap);
String s = HttpUtil.uploadFile(host + url, file, file.getName(), headers, jsonToMap);
return JSONObject.parseObject(s, R.class);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -255,6 +255,76 @@ public class HttpUtil {
return sb.toString();
}
public static String doPost2(String uri,Map<String,Object> params, Map<String, String> headers) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpPost httppost = new HttpPost(uri);
httppost.setHeader("connection", "Keep-Alive");
httppost.setHeader("accept", "*/*");
if (headers != null && headers.size() >0){
Set<String> keySet = headers.keySet();
for (String key : keySet) {
httppost.setHeader(key, headers.get(key));
}
}
if(params!=null&&params.size() > 0){
// HttpParams hp = new BasicHttpParams();
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<Entry<String, Object>> set = params.entrySet();
Iterator<Entry<String, Object>> iterator = set.iterator();
while(iterator.hasNext()){
Entry<String, Object> entry = iterator.next();
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
nvps.add(nvp);
}
try {
if(nvps.size() > 0)
httppost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// Execute the request
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
// System.out.println(response.getStatusLine());
HttpEntity entity = null;
if(response != null){
entity = response.getEntity();
}
if (entity != null) {
InputStream instream = null;
try {
instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream,"UTF-8"));
String temp = null;
while((temp = reader.readLine())!=null){
sb.append(temp);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
httpclient.getConnectionManager().shutdown();
return sb.toString();
}
public static String doPost(String uri,Map<String,String> params) {
HttpClient httpclient = new DefaultHttpClient();
......@@ -472,7 +542,7 @@ public class HttpUtil {
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//加上此行代码解决返回中文乱码问题
builder.addBinaryBody(fileParamName, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
for (Map.Entry<String, String> e : otherParams.entrySet()) {
builder.addTextBody(e.getKey(), e.getValue());// 类似浏览器表单提交,对应input的name和value
builder.addTextBody(e.getKey(), String.valueOf(e.getValue()));// 类似浏览器表单提交,对应input的name和value
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment