1. The legal character types used in base64 encoding are as follows:
- Capital letters A to Z
- Lowercase letters a to z
- Numbers 0 to 9
- Plus sign (+)
- Slash (/)
- Equal sign (=)
Among them, the equal sign (=) is usually used as a Base64-encoded padding character to complete the length of the last encoded block, making it a multiple of 4. In Base64 encoding, the equal sign (=) appears at most two, which are used to complete the 1 or 2 bytes of the last encoding block.
2. If there is a base64-encoded string like below
String bas = "/9j/4AAQSkZJRgABAgAAAQABA*^AD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofH"
There are characters that do not comply with the base64 encoding rules.
Can be usedreplaceAll(String regex,String replacement)Method to remove illegal characters.
public String replaceAll(String regex, String replacement) { return (regex).matcher(this).replaceAll(replacement); }
3. Parameter description
-
regex
: A regular expression to match the parts that need to be replaced. -
replacement
: A string that replaces the matching part.
This method returns a new string where all parts matching the regular expression are replaced with the specified string.
Use bas directly to call it
bas = ("[^A-Za-z0-9+/=]", "");
This regular expression means: match all other characters that are not in the range A-Za-z0-9+/=
This will remove all the illegal characters in the base64-encoded string! !
This is the article about solving the problem of illegal characters appearing in Base64 strings in Java. For more related contents of Base64 illegal characters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!