前端上传图片回显并用base64编码,后端做解码储存,存储图片路径在.properties文件中配置(以上传身份证照片为例)

前端页面:

    
 上传身份证正面照
手持身份证反面照
 
后端代码:
//对字节数组字符串进行Base64解码并生成图片
if (idPhotoUrl == null || idPhotoUrl2 == null) { //图像数据为空
    success(false);
}
 BASE64Decoder decoder = new BASE64Decoder();
byte[] b =new byte[0] ;
if(idPhotoUrl.contains("data:image/png;base64")){
    //Base64解码
    b=decoder.decodeBuffer(idPhotoUrl.replace("data:image/png;base64,", ""));
}
if(idPhotoUrl.contains("data:image/jpeg;base64")){
    //Base64解码
    b=decoder.decodeBuffer(idPhotoUrl.replace("data:image/jpeg;base64,", ""));
}
//idPhotoUrl = idPhotoUrl.replace("data:image/jpeg;base64,", "");
for (int i = 0; i < b.length; ++i) {
    if (b[i] < 0) {//调整异常数据
        b[i] += 256;
    }
}
//生成jpg图片
Properties prop = new Properties();
InputStream ins = this.getClass().getResourceAsStream("/config/photoUrl.properties");
prop.load(ins);
String imgFilePath = prop.getProperty("PHOTOURL") + UUID.randomUUID() + ".jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
user.setIdPhotoUrl(imgFilePath);


byte[] b2 =new byte[0] ;
if(idPhotoUrl2.contains("data:image/png;base64")){
    //Base64解码
    b2=decoder.decodeBuffer(idPhotoUrl2.replace("data:image/png;base64,", ""));
}
if(idPhotoUrl2.contains("data:image/jpeg;base64")){
    //Base64解码
    b2=decoder.decodeBuffer(idPhotoUrl2.replace("data:image/jpeg;base64,", ""));
}
//idPhotoUrl = idPhotoUrl.replace("data:image/jpeg;base64,", "");
for (int i = 0; i < b2.length; ++i) {
    if (b2[i] < 0) {//调整异常数据
        b2[i] += 256;
    }
}
//生成jpg图片
String imgFilePath2 = prop.getProperty("PHOTOURL") + UUID.randomUUID() + ".jpg";//新生成的图片
OutputStream out2 = new FileOutputStream(imgFilePath2);
out2.write(b2);
out2.flush();
out2.close();

user.setIdPhotoUrl2(imgFilePath2);
properties文件:
     PHOTOURL=想放的文件夹绝对路径

你可能感兴趣的:(base64图片加密,base64图片解密,base64,身份证上传,图片上传)