Call Alipay collection queries facial images Base64 decoding

Face recognition result query interface zoloz.identification.user.web.query

Pay imgStr picture string is not a standard format base64 treasure returned,

Not resolve the picture. 
Because the standard Base64 is not suitable for transmission directly into the URL,

Because the URL standard Base64 encoder will the "/" and "+" character becomes shaped like form "% XX" of

So using an improved Base64 encoding the URL,

If you need to turn into a standard image format base64 you need to be converted by the following method. 

base64 1. First Alipay return to first turn into the correct format base64

public static String safeUrlBase64Decode(final String imgStr ) {
    SafeBase64Str.replace base64Str = String ( '-', '+'); 
base64Str = base64Str.replace ( '_', '/');
int Mod4 base64Str.length = (). 4%;
IF (Mod4> 0) {
base64Str base64Str + = "====" the substring (Mod4);.
}
return base64Str;
}

2. then the correct format image base64 decoding
public static File base64ToFile(String base64) {
if(base64==null||"".equals(base64)) {
return null;
}
byte[] buff= Base64.decode(base64);
File file=null;
FileOutputStream fout=null;
try {
file = File.createTempFile("tmp", null);
fout=new FileOutputStream(file);
fout.write(buff);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fout!=null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
3. The above documents will get a file, if you need to file documents into stream
InputStream fileInputStream = new FileInputStream(file);
 

Guess you like

Origin www.cnblogs.com/bt2882/p/11307261.html