Java和PHP配合:deflate(压缩)和inflate(解压)

Java和PHP配合:deflate(压缩)和inflate(解压)
一、Java中deflate压缩发送给php解压缩

Android中deflate代码

1
2
3
4
5
6
OutputStream urlOutStream = urlConnection.getOutputStream();
// 要使用no_wrap的Deflater,php才能解压,9是最高的压缩级别,可以设置为1-9的级别,1速度最快
DeflaterOutputStream deflaterOut = new DeflaterOutputStream(urlOutStream, new Deflater(9, true));
deflaterOut.write((stringToPost[0]).getBytes());
deflaterOut.close();
urlOutStream.close();

PHP中inflate及deflate代码 (有时候终端方法压缩时会自动base64加密一次,php需要先解base64)https://tool.oschina.net/encrypt?type=3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// 首先获取post的字符串:
// 因为是直接写入的压缩字符串,
// 通过$_POST[]并不能获取post内容,
// 可以通过原始请求数据的只读输入流获得post内容
$postStr = file_get_contents('php://input');
 
// Java中默认的Deflater的数据格式有wrap,Java中应设置no_wrap的Deflater
// 如果Java中没有指定no_wrap的Deflater,则PHP中通过下面的算法将wrap去掉,也可以正常解压
// $deflateForPHP = substr($postStr, 2, -4);
 
// 解压缩获得请求的内容
$plainRequest = gzinflate($postStr);
 
// code here ...
 
// 将处理结果压缩后返回请求端
$result = "准备发给android的处理结果";
echo gzdeflate($result);
 
?>

Android中inflate代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream urlInputStream = urlConnection.getInputStream();
// php中gzdeflate()压缩的结果,没有wrap,需要自行计算头尾验证字符,
// 或者指定new Inflater(true)的解压器才能正确解压(注意:没了传输错误校验)
InflaterInputStream inflaterIn = new InflaterInputStream(urlInputStream, new Inflater(true));
BufferedReader reader = new BufferedReader(new InputStreamReader(inflaterIn));
String res = "", line;
while ((line = reader.readLine()) != null) {
res += line;
}
Log.i(TAG, "Response length: " + res.length() + " response: " + res);
reader.close();
inflaterIn.close();
urlInputStream.close();
urlConnection.disconnect();
return res;
}

  

二、完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
private class mHttp extends AsyncTask<String, Void, String> {
private URL mUrl;
private String mMethod = "GET";
private int mHttpTimeout = 3000; /* milliseconds */
private String TAG = "mHttp";
 
public mHttp(String url, String method) throws MalformedURLException {
mUrl = new URL(url);
if(method.toUpperCase().contains("POST")) mMethod = "POST";
}
 
public mHttp(String url) throws MalformedURLException {
mUrl = new URL(url);
}
 
@Override
protected String doInBackground(String... stringToPost) {
if (mUrl != null) {
try {
HttpURLConnection urlConnection = (HttpURLConnection) mUrl.openConnection();
urlConnection.setReadTimeout(mHttpTimeout);
urlConnection.setConnectTimeout(mHttpTimeout);
urlConnection.setRequestMethod(mMethod);
urlConnection.setDoInput(true);
// PHP已经应用pzdeflate()处理了结果,添加Accept-Encoding的header避免Apache再deflate处理
urlConnection.addRequestProperty("Accept-Encoding", "q=1.0 identity");
if (mMethod.equals("POST")) {
urlConnection.setDoOutput(true);
urlConnection.addRequestProperty("Content-Encoding", "deflate");
OutputStream urlOutStream = urlConnection.getOutputStream();
DeflaterOutputStream deflaterOut = new DeflaterOutputStream(urlOutStream, new Deflater(9, true));
deflaterOut.write((stringToPost[0]).getBytes());
deflaterOut.close();
urlOutStream.close();
}
urlConnection.connect();
 
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream urlInputStream = urlConnection.getInputStream();
InflaterInputStream inflaterIn = new InflaterInputStream(urlInputStream, new Inflater(true));
BufferedReader reader = new BufferedReader(new InputStreamReader(inflaterIn));
String res = "", line;
while ((line = reader.readLine()) != null) {
res += line;
}
Log.i(TAG, "Response length: " + res.length() + " response: " + res);
reader.close();
inflaterIn.close();
urlInputStream.close();
urlConnection.disconnect();
return res;
} else {
// show response code.
Log.i(TAG, "doInBackground: responseCode: " + urlConnection.getResponseCode());
 
urlConnection.disconnect();
return null;
}
 
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
 
@Override
protected void onPostExecute(String result) {
Log.i(TAG, "onPostExecute: " + result);
// 处理result字符串,例如显示在UI组件上
// code here...
}
}
mHttp http = new mHttp("http://examle.com/page.php", "post");
http.execute("String to be posted to the server");

  

用到的链接:
php://input 原始请求数据的只读输入流
DeflaterOutputStream
Deflater
InflaterInputStream
Inflater
gzinflate-in-java

posted @   程序生(Codey)  阅读(1433)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示