HttpUtil 跨项目请求

public class HttpUtil {

private static HttpClient httpClient;
// 默认编码
private static String chaset = "UTF-8";

/**
* 初始化HttpClient实例
*/
static {
httpClient = HttpClients.createDefault();
}

/**
* 获取HttpClient实例
*/
public static HttpClient getHttpClient() {
return httpClient;
}

/**
* get请求(不带参数)
*
* @param url
* 请求url
* @return html 页面数据
*/
public static String get(String url) {
HttpGet httpGet = new HttpGet(url);
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* get请求(不带参数)
*
* @param url
* 请求url
* @param headers
* 请求头
* @return html 页面数据
*/
public static String get(String url, Map<String, String> headers) {
HttpGet httpGet = new HttpGet(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* get请求(带参数)
*
* @param url
* 请求url
* @param paramsMap
* get请求参数
* @return html 页面数据
*/
public static String getWithParams(String url, Map<String, String> paramsMap) {
url = url + "?" + parseParams(paramsMap);
HttpGet httpGet = new HttpGet(url);
String html = null;
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return html;
}

/**
* post请求
*
* @param url
* 请求url
* @param paramsMap
* get请求参数
* @return html 页面数据
*/
public static String post(String url, Map<String, String> paramsMap) {
HttpPost httpPost = new HttpPost(url);
String html = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,chaset));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, chaset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return html;
}

/**
* 设置编码
*
* @param chaset
* @return
*/
public static void setCharset(String chaset) {
HttpUtil.chaset = chaset;
}

/**
* 转换参数列表用于get请求
*
* @param paramsMap
* @return
*/
private static String parseParams(Map<String, String> paramsMap) {
String params = "";
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
params += entry.getKey() + "=" + entry.getValue() + "&";
}
return params.substring(0, params.length() - 1);
}

public static String doDelete(String url){
HttpDelete httpdelete = new HttpDelete(url);
HttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpdelete);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
}catch (Exception ex){
ex.printStackTrace();
}
finally {
httpdelete.releaseConnection();
}
return null;
}

/**
* @Title: httpDelete
* @Description: TODO
* @author: wangyingtao
* @date: 2017/8/21 16:22
* @param url
*/
public static String doDelete(String url,Map<String,String> paramsMap){
try {
URIBuilder builder = new URIBuilder(url);
//设置header
if (paramsMap != null && paramsMap.size() > 0) {
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue());
}
}
return doDelete(builder.build().toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}

public static String doUploadFile(String url,MultipartFile[] uploadPics) throws IOException {
HttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

for (MultipartFile uploadPic : uploadPics){
String fileName = uploadPic.getOriginalFilename();
builder.addBinaryBody("upfile[]", uploadPic.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
return EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} finally {
httpPost.releaseConnection();
}
return null;
}


public static void main(String[] args) {
// Map<String, String> paramsMap = new HashMap<String, String>();
// paramsMap.put("community_id", "13525");
// String result = getWithParams("http://api.5i5j.com.cn/v1/communityassess/isexsit", paramsMap);
// System.out.println(result);

Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("uid", "439");
paramsMap.put("type", "2");
paramsMap.put("houseid", "37115758");
String result = HttpUtil.doDelete("http://10.10.137.171/rv/RealTimeVisit/HouseDel", paramsMap);
System.out.println(result);
}

/**
* MultipartFile 转换成File
*
* @param multfile 原文件类型
* @return File
* @throws IOException
*/
public static File multipartToFile(MultipartFile multfile) throws IOException {
CommonsMultipartFile cf = (CommonsMultipartFile)multfile;
//这个myfile是MultipartFile的
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
//手动创建临时文件
// if(file.length() < 2048){
// File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
// file.getName());
// multfile.transferTo(tmpFile);
// return tmpFile;
// }
return file;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值