restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; /**
*
*/
@Configuration
public class RestConfiguration { @Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
//httpRequestFactory.setConnectionRequestTimeout(3000);
httpRequestFactory.setConnectTimeout(30000);
httpRequestFactory.setReadTimeout(300000);
return new RestTemplate(httpRequestFactory);
//return new RestTemplate();
} }
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import java.io.File;
import java.util.Map; /**
*
* 微服务调用通用类(也可用作普通http类用)
*/
public class RestTemplateUtils { /**
* 发送get请求
* @param url
* @param params
* @param headers
* @param responseType
* @param <T>
* @return
*/
public static <T> T doHttpGet(String url, Map<String, String> params, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
RestTemplate restTemplate = getRestTemplate(isSpringCloud);
HttpHeaders restHeaders = new HttpHeaders();
restHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode()); if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
restHeaders.add(entry.getKey(), entry.getValue());
}
} if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
if (url.contains("?")) {
url = url + "&" + entry.getKey() + "=" + entry.getValue();
} else {
url = url + "?" + entry.getKey() + "=" + entry.getValue();
}
}
}
HttpEntity<String> restRequest = new HttpEntity<>(null, restHeaders); ResponseEntity<T> result = restTemplate.exchange(url, HttpMethod.GET, restRequest, responseType); return result.getBody();
//return restTemplate.getForObject(url, responseType, restRequest);
} /**
* 发送post请求
* @param url
* @param params
* @param headers
* @param responseType
* @param <T>
* @return
*/
public static <T> T doHttpPost(String url, Map<String, String> params, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
RestTemplate restTemplate = getRestTemplate(isSpringCloud);
HttpHeaders restHeaders = new HttpHeaders();
MultiValueMap<String, Object> restParam = new LinkedMultiValueMap<>(); restHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode()); if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
restHeaders.add(entry.getKey(), entry.getValue());
}
} if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
restParam.add(entry.getKey(), entry.getValue());
}
} HttpEntity<MultiValueMap<String, Object>> restRequest = new HttpEntity<>(restParam, restHeaders);
return restTemplate.postForObject(url, restRequest, responseType);
} /**
* 微服务url格式 String url = "http://服务名/provider/xxx";
* 上传文件
* @param url
* @param params
* @param files
* @param headers
* @param responseType
* @param <T>
* @return
*/
public static <T> T doUploadFile(String url, Map<String, String> params, Map<String, File> files, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
RestTemplate restTemplate = getRestTemplate(isSpringCloud);
HttpHeaders restHeaders = new HttpHeaders();
MultiValueMap<String, Object> restParam = new LinkedMultiValueMap<>(); restHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode()); if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
restHeaders.add(entry.getKey(), entry.getValue());
}
} if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
restParam.add(entry.getKey(), entry.getValue());
}
} if (files != null) {
for (Map.Entry<String, File> entry : files.entrySet()) {
restParam.add(entry.getKey(), new FileSystemResource(entry.getValue()));
}
}
HttpEntity<MultiValueMap<String, Object>> restRequest = new HttpEntity<>(restParam, restHeaders);
return restTemplate.postForObject(url, restRequest, responseType);
} /**
* 获取RestTemplate
* @param isSpringCloud
* @return
*/
public static RestTemplate getRestTemplate(boolean isSpringCloud){
RestTemplate restTemplate = null;
if(isSpringCloud){
restTemplate = SpringContextHolder.getBean(RestTemplate.class);
/*HttpComponentsClientHttpRequestFactory httpRequestFactory = ((HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory());
httpRequestFactory.setConnectTimeout(3000);
httpRequestFactory.setReadTimeout(300000);*/
}else{
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(30000);
httpRequestFactory.setReadTimeout(60000);
restTemplate = new RestTemplate(httpRequestFactory);
}
return restTemplate;
} }
restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)的更多相关文章
- SpringMVC实现PUT请求上传文件
在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...
- Postman Post请求上传文件
Postman Post请求上传文件一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type :Value:multipart/form-data 如下图 三. ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- python中使用multipart/form-data请求上传文件
最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- python发送post请求上传文件,无法解析上传的文件
前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...
- Java客户端通过Http发送POST请求上传文件到web服务器
http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html 1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.n ...
- JAVA模拟HTTP post请求上传文件
在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...
- 通过PHP CURL模拟请求上传文件|图片。
现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...
- 通过POST请求上传文件
转自:https://blog.csdn.net/zhangge3663/article/details/81218488 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交 ...
随机推荐
- 防刷功能的实现(thinkphp5)
$seconds = '3'; //时间段[秒] $refresh = '3';//最大次数 $cur_time = time(); if(Session::get('refresh_times')) ...
- web带宽估算方法
每个连接约占用10Kb的带宽,以3万总用户数和10%的在线率计算,并按照10%的冗余率,服务器总带宽=每秒总连接数*10Kbps /(1-冗余率)/1024. 带宽占用(Mbps)=30000*10% ...
- 案例分享 | dubbo 2.7.12 bug导致线上故障
本文已收录 https://github.com/lkxiaolou/lkxiaolou 欢迎star.搜索关注微信公众号"捉虫大师",后端技术分享,架构设计.性能优化.源码阅读. ...
- P5437-[XR-2]约定【拉格朗日差值,数学期望】
正题 题目链接:https://www.luogu.com.cn/problem/P5437 题目大意 \(n\)个点的完全图,连接\(i,j\)的边权值为\((i+j)^k\).随机选出一个生成树, ...
- 在modal中的datetimepicker插件BUG修复
前言:因为在模态框用到datetimepicker这一日期控件,而选中日期时,会触发模态框的再次打开,导致上面表单选的值会重新加载 解决办法: 用stopPropagation() 方法阻止事件传播, ...
- vue 快速入门 系列 —— vue-router
其他章节请看: vue 快速入门 系列 Vue Router Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌. 什么是路由 ...
- 洛谷3176 [HAOI2015]数字串拆分 (矩阵乘法+dp)
qwq真的是一道好题qwq自己做基本是必不可能做出来的. 首先,如果这个题目只是求一个\(f\)数组的话,那就是一道裸题. 首先,根据样例 根据题目描述,我们能发现其实同样数字的不同排列,也是属于不同 ...
- 洛谷4630APIO2018铁人两项(圆方树+dp)
QWQ神仙题啊(据说是今年第一次出现圆方树的地方) 首先根据题目,我们就是求对于每一个路径\((s,t)\)他的贡献就是两个点之间的点数,但是图上问题我并没有办法很好的解决... 这时候考虑圆方树,我 ...
- Salesforce 生命周期管理(一)应用生命周期浅谈
本篇参考: https://trailhead.salesforce.com/en/content/learn/trails/determine-which-application-lifecycle ...
- Java生成6位数验证码
public static String getCode() { return String.valueOf((int) ((Math.random() * 9 + 1) * 100000));} 生 ...