Nin*_*rni 18 java string base64 byte encoder-decoder
我试图将base64字符串转换为字节数组,但它抛出以下错误
java.lang.IllegalArgumentException:非法base64字符3a
我试过以下选项userimage是base64字符串
byte[] img1 = org.apache.commons.codec.binary.Base64.decodeBase64(userimage);`
/* byte[] decodedString = Base64.getDecoder().decode(encodedString.getBytes(UTF_8));*/
/* byte[] byteimage =Base64.getDecoder().decode( userimage );*/
/* byte[] byteimage = Base64.getMimeDecoder().decode(userimage);*/`
Run Code Online (Sandbox Code Playgroud)
小智 30
您可以使用java.util.Base64包来解码String byte[].下面我用于编码和解码的代码.
对于Java 8:
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Example {
public static void main(String[] args) {
try {
byte[] name = Base64.getEncoder().encode("hello World".getBytes());
byte[] decodedString = Base64.getDecoder().decode(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
对于Java 6:
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String[] args) {
try {
byte[] name = Base64.encodeBase64("hello World".getBytes());
byte[] decodedString = Base64.decodeBase64(new String(name).getBytes("UTF-8"));
System.out.println(new String(decodedString));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50725 次 |
| 最近记录: |