package com.zhang;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpUtils {
public static void main(String[] args) {
Donwload("http://127.0.0.1:8888/HttpServer/img/aaa.jpg","C://ayy哈哈a.jpg");
/*Map<String, Object> map=new HashMap<String, Object>();
map.put("employee_no", 18811);
Upload("http://127.0.0.1:8888/HttpServer/Upload","C://aa.jpg" , map);*/
/* Map<String, String> map=new HashMap<String, String>();
map.put("id", "哈哈哈");
String json=GetData("http://127.0.0.1:8888/HttpServer/getUser",map);
System.out.println(json);*/
/*JSONObject json =new JSONObject();
json.put("name", "哈哈");
String j=PostData("http://127.0.0.1:8888/HttpServer/saveUser",json.toJSONString());
System.out.println(j);*/
}
public static String GetData(String url,Map<String, String> map) {
String result="";
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
for (Entry<String, String> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String param = "?"+URLEncodedUtils.format(params, "utf-8"); //对参数编码
HttpGet httpget = new HttpGet(url+param); // 创建httpget.
response = httpclient.execute(httpget); // 执行get请求.
HttpEntity entity = response.getEntity(); // 获取响应实体
int StatusCode=response.getStatusLine().getStatusCode(); // 打印响应状态码
if (StatusCode==200 && entity != null) {
result=EntityUtils.toString(entity,"UTF-8"); // 打印响应内容
/* JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println(jsonObject.get("success"));
User user= JsonUtils.JsonToBean(jsonObject.get("user").toString());
System.out.println(user);
System.out.println(JsonUtils.ToJson(user));*/
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String PostData(String url, String jsonString) {
String result="";
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = HttpClients.createDefault();// 创建默认的httpClient实例
HttpPost httppost = new HttpPost(url); // 创建httppost
StringEntity stringEntity = new StringEntity(jsonString,"utf-8");//解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httppost.setEntity(stringEntity);
try {
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
int StatusCode=response.getStatusLine().getStatusCode();
if (StatusCode == 200 && entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}else{
result = EntityUtils.toString(entity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String Upload(String url,String fileName,Map<String, Object> map) {
String result="";
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder builder=MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));//设置请求的编码格式
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式
FileBody bin = new FileBody(new File(fileName));
builder.addPart("file",bin); //文件流参数
ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
for (Entry<String, Object> entry : map.entrySet()) {
builder.addPart(entry.getKey(),new StringBody(entry.getValue().toString(), contentType));//普通的参数
}
HttpEntity reqEntity= builder.build();
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
int status=response.getStatusLine().getStatusCode();
HttpEntity resEntity = response.getEntity();
if (status==200 && resEntity != null) {
result= EntityUtils.toString(resEntity,"UTF-8");
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static void Donwload(String url, String destFileName) {
InputStream in = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
in = entity.getContent();
File file = new File(destFileName);
FileOutputStream fout = new FileOutputStream(file);
int l = -1;
byte[] tmp = new byte[1024];
while ((l = in.read(tmp)) != -1) {
fout.write(tmp, 0, l); // 注意这里如果用OutputStream.write(buff)的话,图片会失真
}
fout.flush();
fout.close();
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
HttpClient的简单使用,get、post、上传、下载
共58个文件
jar:36个
xml:4个
class:3个
1星 需积分: 10 28 下载量 96 浏览量
2017-12-06
13:58:00
上传
评论 2
收藏 11MB ZIP 举报
温馨提示
HttpClient的简单使用,get、post、上传、下载。包含服务端和客户端代码
资源推荐
资源详情
资源评论
收起资源包目录
HttpClient.zip (58个子文件)
HttpClient
.project 386B
src
com
zhang
HttpUtils.java 7KB
lib
httpclient-cache-4.5.3.jar 156KB
fluent-hc-4.5.3.jar 30KB
fastjson-1.2.3.jar 400KB
httpcore-4.4.6.jar 316KB
httpclient-4.5.3.jar 730KB
jna-4.1.0.jar 893KB
log4j-1.2.8.jar 344KB
httpclient-win-4.5.3.jar 17KB
jna-platform-4.1.0.jar 1.4MB
commons-logging-1.2.jar 60KB
commons-codec-1.9.jar 258KB
httpmime-4.5.3.jar 40KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 1KB
bin
com
zhang
HttpUtils.class 9KB
HttpServer
.project 910B
WebContent
WEB-INF
lib
servlet-api.jar 130KB
spring-context-4.0.0.RELEASE.jar 928KB
log4j-1.2.16.jar 470KB
spring-expression-4.0.0.RELEASE.jar 202KB
spring-tx-4.0.0.RELEASE.jar 242KB
spring-webmvc-4.0.0.RELEASE.jar 645KB
jstl.jar 17KB
spring-web-4.0.0.RELEASE.jar 646KB
standard-1.1.2.jar 384KB
spring-aop-4.0.0.RELEASE.jar 343KB
spring-beans-4.0.0.RELEASE.jar 654KB
commons-logging-1.1.1.jar 59KB
spring-test-4.0.0.RELEASE.jar 431KB
jackson-core-2.6.0-xh.jar 253KB
spring-core-4.0.0.RELEASE.jar 933KB
jackson-databind-2.6.0-xh.jar 1.11MB
spring-jdbc-4.0.0.RELEASE.jar 410KB
spring-context-support-4.0.0.RELEASE.jar 131KB
spring-aspects-4.0.0.RELEASE.jar 50KB
commons-fileupload-1.3.jar 67KB
aopalliance.jar 4KB
commons-io-2.4.jar 181KB
jackson-jr-all-2.4.3-xh.jar 292KB
jackson-annotations-2.6.0-xh.jar 46KB
web.xml 2KB
img
aaa.jpg 133KB
META-INF
MANIFEST.MF 39B
src
com
zhang
model
User.java 998B
controller
UserController.java 4KB
config
spring-mvc.xml 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 482B
.jsdtscope 567B
build
classes
com
zhang
model
User.class 2KB
controller
UserController.class 5KB
config
spring-mvc.xml 2KB
.classpath 833B
共 58 条
- 1
资源评论
- rui_dir2018-06-23没啥用 差评。。。。
zhangjinru
- 粉丝: 0
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python的PCA人脸识别算法的原理及实现代码详解+源码+详细代码解析+开发文档+数据(毕业设计&课程设计&项目开发)
- Decision tree20240105(1).ipynb
- zuoyezuoyezuoye
- zuoyezuoyezuoye
- 机械设计电机转子装配设备sw22非常好的设计图纸100%好用.zip
- 作业作业作业作业作业作业
- xdotool.c
- RLMD鲁棒性局部均值分解信号分量可视化(Matlab完整源码和数据)
- Screenshot_2024-04-26-17-17-26-36_9d26c6446fd7bb8e41d99b6262b17def.jpg
- 6.0版本超广角文件+教程使用MT管理器打-7.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功