对图片使用base64编码

对图片进行base64编码时 , 发现将图片流转为String再编码会无法解码
转成byte[] 进行编码可以正常解码

/**
     * @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;
    }

猜你喜欢

转载自blog.csdn.net/Evain_Wang/article/details/112848712