Use base64 encoding for pictures

When encoding the picture in base64, it is found that converting the picture stream to String and then encoding will not be able to decode.
Converted to byte[] for encoding, it can be decoded normally

/**
     * @Description:输入流转换为字节数组
     * @Date: 2020/12/16 20:01
     * @Param: [inputStream, encode]
     * @Return: java.lang.String
     **/
    private byte[] transInputStreamToString(InputStream inputStream) {
    
    
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        byte[] result = null;
        if (inputStream != null) {
    
    
            try {
    
    
                while ((len = inputStream.read(data)) != -1) {
    
    
                    outputStream.write(data, 0, len);
                }
                result = outputStream.toByteArray();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        return result;
    }
/**
     * @Description:对字节数组base64加密
     * @Date: 2020/12/16 20:04
     * @Param: [value, charSet]
     * @Return: java.lang.String
     **/
    public String base64EncodeString(byte[] value) {
    
    
        String encodeValue = null;
        if (value.length != 0 ) {
    
    
            try {
    
    
                encodeValue = (new BASE64Encoder()).encode(value);
            } catch (Exception var4) {
    
    
             
            }
        }

        return encodeValue;
    }

Guess you like

Origin blog.csdn.net/Evain_Wang/article/details/112848712