webapp
2019-10-02 22df9eba843e23701f150cf02dacd2dd050c1c6b
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
package com.java110.utils.util;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import java.io.*;
 
public class Base64Convert {
 
    private void Base64Convert() {
 
    }
 
    /**
     * 流转换为字符串
     *
     * @param in
     * @return
     * @throws IOException
     */
    public static String ioToBase64(InputStream in) throws IOException {
        String strBase64 = null;
        try {
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            // 将文件中的内容读入到数组中
            in.read(bytes);
            strBase64 = new BASE64Encoder().encode(bytes);      //将字节流数组转换为字符串
        } finally {
            if (in != null) {
                in.close();
            }
        }
 
        return strBase64;
    }
 
    /**
     * 将base64 转为字节
     * @param strBase64
     * @return
     * @throws IOException
     */
    public static byte[] base64ToByte(String strBase64) throws IOException {
        // 解码,然后将字节转换为文件
        byte[] bytes = new BASE64Decoder().decodeBuffer(strBase64);   //将字符串转换为byte数组
        return bytes;
    }
}