5.post上传和压缩、插件模拟请求
gzip
gzip一种压缩方式,或者是文件形式,它主要用于网络传输数据的压缩
gzip压缩好不好用
- 浏览器:网速一定.内容越小.请求响应的速度是不是更快
- 手机server:返回数据类型是json/xml->文本-->压缩率会很高.
gzip执行流程
//1. 告知服务器.客户端支持gzip解压
* get.addHeader("Accept-Encoding", "gzip");//2. 根据响应头得知服务器知否进行了gzip压缩
* Header[] headers = response.getHeaders("Content-Encoding"); for (Header header : headers) { if ("gzip".equals(header.getValue())) { isUseGzip = true; } }//3.根据是否使用gzip.解压具体的数据
if (isUseGzip) { GZIPInputStream in = new GZIPInputStream(entity.getContent()); result = IoUtils.convertStreamToString(in); } else { result = EntityUtils.toString(entity); }服务端具体怎么支持gzip压缩.根据使用的服务器语言来定
post请求参数的形式
key-value--->表单:结构比较单一.而且比较繁琐
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); for (String key : parmas.keySet()) { BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, parmas.get(key)); parameters.add(basicNameValuePair); } UrlEncodedFormEntity form = new UrlEncodedFormEntity(parameters); post.setEntity(form);jsonString形式:结构灵活.实际开发用的很多
post.setEntity(new StringEntity(jsonString));file形式:需要jar包:
很多框架上传图片就是用他MultipartEntity entity = new MultipartEntity(); entity.addPart("actimg", new FileBody(file)); post.setEntity(entity);- 多张图片上传:写个循环,用键值,可以用BasicNameValuePair,也可以用file,循环添加,也可以用base64变成string上传

- Base64:byte[]-->string
- 上传图片,语音:
- 存对象到sp:如map
iv = (ImageView) findViewById(R.id.iv);
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//2. bitmap-->byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();
//3.byte[]->string,这样就可以用string传给服务器
String bitmapString = Base64.encodeToString(byteArray, Base64.DEFAULT);
System.out.println("bitmapString :" + bitmapString);
//4. string-->byte[];
byte[] bitmapByteArr = Base64.decode(bitmapString, Base64.DEFAULT);
//5.byte[]-->bitmap
Bitmap bitmap2 = BitmapFactory.decodeByteArray(bitmapByteArr, 0, bitmapByteArr.length);
//6.在imageView上设置图片
iv.setImageBitmap(bitmap2);
//obj-->byte[] -->string->sp
}
});
post的content-type
- application/x-www-form-urlencoded :表单类型-->key-value,默认
- multipart/form-data :文件上传
- application/json :jsonString
- text/xml :xml
- text/plain:文本
- post.addHeader("Content-Type", "application/json");//如果后台固定死了.不加请求头.同样返回json那就没问题.但是如果后台逻辑和这个请求头有关系.就必须要加上.可以肯定的说.加了万无一失
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGzip();
Map<String, String> map = new HashMap<String, String>();
map.put("name", "billy");
initPostKeyValue(map);
String jsonString = "{\"name\":\"billy\"}";
initPostJsonString(jsonString);
File file = new File("");
initPostFile(file);
}
/**
* 文件的上传
* @param file
*/
private void initPostFile(File file) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
//2. 传递二进制类型的参数
MultipartEntity entity = new MultipartEntity();
entity.addPart("actimg", new FileBody(file));
post.setEntity(entity);
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用jsonString post数据库
* @param jsonString
*/
private void initPostJsonString(String jsonString) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
post.addHeader("Content-Type", "application/json");
//Content-Type text/xml-->xml
//Content-Type application/json-->json
//2. 传递jsonString类型的参数
post.setEntity(new StringEntity(jsonString));
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用key-value的形式post数据
* @param parmas 请求参数对于的map集合
*/
private void initPostKeyValue(Map<String, String> parmas) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
//1. 传递key-value的参数
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
for (String key : parmas.keySet()) {
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, parmas.get(key));
parameters.add(basicNameValuePair);
}
UrlEncodedFormEntity form = new UrlEncodedFormEntity(parameters);
post.setEntity(form);
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initGzip() {
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
boolean isUseGzip = false;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://httpbin.org/gzip");
//1. 告知服务器.客户端支持gzip解压
get.addHeader("Accept-Encoding", "gzip");
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
//2. 根据响应头得知服务器知否进行了gzip压缩
Header[] headers = response.getHeaders("Content-Encoding");
for (Header header : headers) {
if ("gzip".equals(header.getValue())) {
isUseGzip = true;
}
}
HttpEntity entity = response.getEntity();
//3.根据是否使用gzip.解压具体的数据
String result = "";
if (isUseGzip) {
GZIPInputStream in = new GZIPInputStream(entity.getContent());
result = IoUtils.convertStreamToString(in);
} else {
result = EntityUtils.toString(entity);
}
System.out.println("result:" + result);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}
restclient(firfox插件),postman(谷歌浏览器)
网络请求模拟插件,post用,get直接在浏览器网页就打开了
5.post上传和压缩、插件模拟请求的更多相关文章
- 利用canvas对上传图片进行上传前压缩
利用谷歌调式工具发现,图片大小直接影响着首屏加载时间. 且考虑到后期服务端压力,图片压缩特别必要. 本文是前端利用canvas实现图片.参考文章:https://www.cnblogs.com/007 ...
- bootstrap-fileinput上传文件的插件使用总结----编辑已成功上传过的图片
http://plugins.krajee.com/file-plugin-methods-demo 具体操作 http://plugins.krajee.com/file-preview-manag ...
- PHP实现图片上传并压缩
本文实例讲解了PHP图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下 使用到三个文件 connect.php:连接数据库 test_upload.php:执行SQL语句 upload_im ...
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...
- Nodejs实现图片的上传、压缩预览、定时删除
前言 我们程序员日常都会用到图片压缩,面对这么常用的功能,肯定要尝试实现一番.第一步,node基本配置 这里我们用到的是koa框架,它可是继express框架之后又一个更富有表现力.更健壮的web框架 ...
- 百度编辑器上传大视频报http请求错误怎么办
百度编辑器UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,所以受到很多开放人员的青睐.但是有时 ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求
1. 示例代码 using System; using System.IO; using System.Net.Http; /// <summary> /// 下载文件 /// </ ...
- post上传和压缩、插件模拟请求
gzip gzip一种压缩方式,或者是文件形式,它主要用于网络传输数据的压缩 gzip压缩好不好用 浏览器:网速一定.内容越小.请求响应的速度是不是更快 手机server:返回数据类型是json/ ...
随机推荐
- C# 多线程通信详解
一.WaitHandler的类层次 可以看到 WaitHandle是 事件(EventWaitHandle).互斥体(Mutex).信号量(Sempahore)的父类. WaitHandle我们最经常 ...
- 两周“学会”bootstrap搭建一个移动站点
一直想着用bootstrap搭建网站,它的自适应.元素封装完善.现成的Glyphicons字体图标,省去很多的css.js.ui的工作,可以快速搭建一个客户需要的站点.ytkah自己有一些div+cs ...
- 什么是元数据(Metadata)?
什么是元数据 任何文件系统中的数据分为数据和元数据.数据是指普通文件中的实际数据,而元数据指用来描述一个文件的特征的系统数据,诸如访问权限.文件拥有者以及文件数据块的分布信息(inode ...
- 速度之王 — LZ4压缩算法(三)
LZ4使用 make / make clean 得到可执行程序:lz4.lz4c Usage: ./lz4 [arg] [input] [output] input : a filename Argu ...
- GIT的标准文档 使用和服务介绍
http://www.kancloud.cn/kancloud/how-to-use-github/42192 1. 探索GitHub 熟悉Git的人几乎都知道并喜欢GitHub,反过来GitHub也 ...
- AWS AutoScaling
origin_from: http://blog.csdn.net/libing_thinking/article/details/48327189 AutoScaling 是 AWS 比较核心的一个 ...
- VMware Snapshot 工作原理
VMware中的快照是对VMDK在某个时间点的“拷贝”,这个“拷贝”并不是对VMDK文件的复制,而是保持磁盘文件和系统内存在该时间点的状态,以便在出现故障后虚拟机能够恢复到该时间点.如果对某个虚拟机创 ...
- 【OpenStack】OpenStack系列16之OpenStack镜像制作
参考 参考: https://www.google.com.hk/?gws_rd=ssl#safe=strict&q=openstack+img+%E5%88%B6%E4%BD%9C http ...
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续
目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...
- js 函数声明方式以及javascript的历史
1.function xx(){} 2.匿名方式 window.onload=function(){dslfjdslfkjdslf}; 3.动态方式 var demo=new Function ...
很多框架上传图片就是用他