Skip to content

Commit 4c9104c

Browse files
committed
updates
1 parent eabea47 commit 4c9104c

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

第8章 网络编程/网络编程.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,17 @@ public class Tools {
11001100

11011101
当使用Get请求的时候,请求参数是跟在url的后面,但是url中不能出现中文,如果参数中带有中文,需要使用URLEncoder编码
11021102

1103+
地址栏不允许中文,传递一些特殊字符 & ?
1104+
11031105
```java
1104-
URLEncoder.encode(); // 编码
1105-
URLDecoder.decode(); // 解码
1106+
String url = "http://www.baidu.com?serarch=\"哈哈\"&key=value";//& ?
1107+
//URLEncoder encode
1108+
String encode = URLEncoder.encode(url);
1109+
System.out.println(encode);
1110+
//URLDecoder decode
1111+
String decode = URLDecoder.decode(encode);
1112+
System.out.println(decode);
1113+
//没有经过encode的字符串.直接decode,会原样输出
1114+
String decode2 = URLDecoder.decode(url);
1115+
System.out.println(decode2);
11061116
```

第9章 安全加密/base64.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Base64的原理很简单,首先,准备一个包含64个字符的数组:
66

77
```
88
['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... '+', '/']
9-
109
```
1110

1211
![Base64 字符映射表](http://img.blog.csdn.net/20160910122745327)
@@ -21,29 +20,8 @@ Base64的原理很简单,首先,准备一个包含64个字符的数组:
2120

2221
如果要编码的二进制数据不是3的倍数,最后会剩下1个或2个字节怎么办?Base64用`\x00`字节在末尾补足后,再在编码的末尾加上1个或2个`=`号,表示补了多少字节,解码的时候,会自动去掉。
2322

24-
Python内置的`base64`可以直接进行base64的编解码:
25-
26-
```
27-
>>> import base64
28-
>>> base64.b64encode('binary\x00string')
29-
'YmluYXJ5AHN0cmluZw=='
30-
>>> base64.b64decode('YmluYXJ5AHN0cmluZw==')
31-
'binary\x00string'
32-
33-
```
34-
3523
由于标准的Base64编码后可能出现字符`+``/`,在URL中就不能直接作为参数,所以又有一种"url safe"的base64编码,其实就是把字符`+``/`分别变成`-``_`
3624

37-
```
38-
>>> base64.b64encode('i\xb7\x1d\xfb\xef\xff')
39-
'abcd++//'
40-
>>> base64.urlsafe_b64encode('i\xb7\x1d\xfb\xef\xff')
41-
'abcd--__'
42-
>>> base64.urlsafe_b64decode('abcd--__')
43-
'i\xb7\x1d\xfb\xef\xff'
44-
45-
```
46-
4725
还可以自己定义64个字符的排列顺序,这样就可以自定义Base64编码,不过,通常情况下完全没有必要。
4826

4927
Base64是一种通过查表的编码方法,不能用于加密,即使使用自定义的编码表也不行。
@@ -53,26 +31,41 @@ Base64适用于小段内容的编码,比如数字证书签名、Cookie的内
5331
由于`=`字符也可能出现在Base64编码中,但`=`用在URL、Cookie里面会造成歧义,所以,很多Base64编码后会把`=`去掉:
5432

5533
```
56-
# 标准Base64:
34+
// 标准Base64:
5735
'abcd' -> 'YWJjZA=='
58-
# 自动去掉=:
36+
// 自动去掉=:
5937
'abcd' -> 'YWJjZA'
6038
6139
```
6240

6341
去掉`=`后怎么解码呢?因为Base64是把3个字节变为4个字节,所以,Base64编码的长度永远是4的倍数,因此,需要加上`=`把Base64字符串的长度变为4的倍数,就可以正常解码了。
6442

65-
请写一个能处理去掉`=`的base64解码函数:
43+
### 应用
6644

67-
```
68-
>>> base64.b64decode('YWJjZA==')
69-
'abcd'
70-
>>> base64.b64decode('YWJjZA')
71-
Traceback (most recent call last):
72-
...
73-
TypeError: Incorrect padding
74-
>>> safe_b64decode('YWJjZA')
75-
'abcd'
45+
把一些对象转换成string,用处:传输的时候不要明文传输
46+
47+
- 上传图片,上传语音
48+
- 如何把一个map存到sp-->Base64支持把byte[]-->String,只需把对象先转换成byte[]就可以存到sp中
49+
50+
```java
51+
ImageView iv = (ImageView) findViewById(R.id.iv);
52+
//1.得到bitmap
53+
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
54+
//2.bitmap-->byte[]
55+
ByteArrayOutputStream out = new ByteArrayOutputStream();
56+
bitmap.compress(CompressFormat.PNG, 100, out);
57+
byte[] bitmapByteArr = out.toByteArray();
58+
//3.使用base64 byte[]--String--->上传到服务器
59+
String bitmapBase64String = Base64.encodeToString(bitmapByteArr, Base64.DEFAULT);
60+
61+
//key-value jsonString
62+
63+
//4.String-->byte[]
64+
byte[] bitmapByteArr2 = Base64.decode(bitmapBase64String, Base64.DEFAULT);
65+
//5.byte[]-->Bitmap -->完成图片的上传
66+
Bitmap bitmapPassed = BitmapFactory.decodeByteArray(bitmapByteArr2, 0, bitmapByteArr2.length);
67+
//6.设置图片到imageView
68+
iv.setImageBitmap(bitmapPassed);
7669
```
7770

7871
### 小结

0 commit comments

Comments
 (0)