wlzboy
5 天以前 fe33646ee6e2d1e57f2b51812e94983a0e9efb04
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
package com.ruoyi.common.utils;
 
 
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;
 
public class InputStreamBase64Converter {
 
    /**
     * 将InputStream转换为Base64字符串
     */
    public static String inputStreamToBase64(InputStream inputStream) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead;
 
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
 
        byte[] bytes = outputStream.toByteArray();
        return Base64.getEncoder().encodeToString(bytes);
    }
 
    /**
     * 将Base64字符串转换回InputStream
     */
    public static InputStream base64ToInputStream(String base64String) {
        byte[] bytes = Base64.getDecoder().decode(base64String);
        return new ByteArrayInputStream(bytes);
    }
}