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请求提交 ...
随机推荐
- Elasticsearch、XXLJob以及最近的学习记录
Elasticsearch.XXLJob以及最近的学习记录 前言 在这九月的最后一周,来总结一下最近的学习记录,主要是对于Elasticsearch.XXLjob的初步学习,想着还是多记录点,以便后面 ...
- 鸿蒙内核源码分析(VFS篇) | 文件系统和谐共处的基础 | 百篇博客分析OpenHarmony源码 | v68.01
子曰:"质胜文则野,文胜质则史.文质彬彬,然后君子." <论语>:雍也篇 百篇博客系列篇.本篇为: v68.xx 鸿蒙内核源码分析(VFS篇) | 文件系统和谐共处的基 ...
- 鸿蒙内核源码分析(信号生产篇) | 信号安装和发送过程是怎样的? | 百篇博客分析OpenHarmony源码 | v48.03
百篇博客系列篇.本篇为: v48.xx 鸿蒙内核源码分析(信号生产篇) | 年过半百,依然活力十足 | 51.c.h .o 进程管理相关篇为: v02.xx 鸿蒙内核源码分析(进程管理篇) | 谁在管 ...
- P3180-[HAOI2016]地图【圆方树,莫队,分块】
正题 题目链接:https://www.luogu.com.cn/problem/P3180 题目大意 \(n\)个点\(m\)条边的一个仙人掌,有点权. \(Q\)次询问给出\(op,x,y\),封 ...
- 无法解析的外部符号"void_cdecl caffe::caffe_gpu_dot<double>(int,double........)"
将源码中的.cu文件添加到项目中即可,即使创建的就是NVIDIA的项目,也需要把这些个.cu文件添加进来
- 腾讯混合云存储 TStor 系列再添新成员,并行存储一体机正式发布
最近国内某大型互联网公司依靠其数据优势成功上市,可见数据的重要性,而数据和存储密不可分,您真的知道自己需要更高性能存储吗? 在当今数据爆发式增长的时代,数据已经成为很多行业最重要的资源,没有之一. 数 ...
- LeetCode352 将数据流变为多个不相交区间
LeetCode352 将数据流变为多个不相交区间 1 题目 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表. 实现 Summa ...
- pycharm中安装和使用sqlite过程详解
创建Django项目,添加app 使用虚拟环境 项目创建默认使用的Django数据库是sqlite 配置静态文件 STATIC_URL = '/static/' # HTML中使用的静态文件夹前缀 S ...
- Head First Python 代码和实例下载
http://python.itcarlow.ie/resources.html
- javascript-原生-闭包
1.变量的作用域 前提:这里只全部都通过var创建的变量或对象 1.全局变量:函数外创建变量 var x=10; function test(){ alert("全局变量在test函数中&q ...