在发送到Web服务器之前是否可以检查Base64图像.Web服务器接受base64图像,因此我可以通过以下方法转换图像:
public void makeJSON_For_Image(int imageSize, String img_Address, String guid) {
imageSizes = imageSize;
img_Address = img_Address.substring(7, img_Address.length());
byte[] bytes = null;
byte[] buffer = new byte[8192];
int bytesRead;
try {
InputStream input = new FileInputStream(img_Address);
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
bytes = output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
LoggerExeption.log(e.getMessage() + "\n", e.getStackTrace());
}
String pic = Base64.encodeToString(bytes, Base64.DEFAULT);
}
但是有时候,服务出错,因为它无法将base64转换为图像。
是否可以检查是否正确转换了base64?
根据Alexandru Sandu
的建议,我使用了如下正则表达式:
String pic = Base64.encodeToString(bytes, Base64.DEFAULT);
String regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
if(pic.matches(regex)){
System.out.println("it's a Base64");
}
但是我的if条件返回 false !!! 这是我的base46的一部分:
pic =
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYF
BgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoK
CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAUAA8ADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwBtte/Z
4xAIcKowMCrEeoWz5WRRSG2i2+YhyMUi2UEwPOK/Pz7Qe1pbTjfE4AqNtOZcmKTPtTfslzFnyJMj
0zTVlukYiRelAEcsc8R5FIJkkXbIuCKtJcRyriUUjWccmSjjHoaB30K4Kj5Q2aQLNC28DINPk09l
+ZentUZaaLjOQKAWpKl0MESIDSiS3kHyAZqHzkIww/So2MYOUPWgLIsMkEnDAGmfZYlOFJH1pitF
nIkwfenRsRkM+R7GgQjWsmDsbI7iql1pVpKC0tsoP95OKviUJyvWkyJAS2D65oAx/wCyZRn7FeYI
/heml9YsgfPtyQP4k5Fa/kwsCE4I7ikEbrwGzQUmjNg1i3lylwOffrUyfZZM7WxUk9pZXGVuLdT7
似乎不正确!
但是我在this在线base 64检查器中检查了我的照片,我的图像完全出现了...