* base64转图片 //对字节数组字符串进行Base64解码并生成图片
* @param base64str base64码
* @return
// @param savePath 图片路径private static final String savePath="image_ver\\verifyCode";
判断是否为base64编码
public static void mainDDD(String[] args) throws JsonProcessingException {String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";if (stringBase64.lastIndexOf(",") != -1) {stringBase64= stringBase64.substring(stringBase64.lastIndexOf(",") + 1);}Boolean isLegal = stringBase64.matches(base64Pattern);if (!isLegal) {System.out.println("输入的不是Base64编码的字符串。");}return;}
生成base64编码,处理字符串 //Base64解码
判断base63str是否为空
private static boolean ifBase64NotNull(String base64str){if (base64str == null) {return false;}System.out.println(true);return true;}
BASE64Decoder decoder = new BASE64Decoder(); log.info(decoder); //开始解码 byte[] b = decoder.decodeBuffer(base64str);
public static boolean GenerateImage(String base64str) throws IOException {ifBase64NotNull(base64str);BASE64Decoder decoder = new BASE64Decoder();byte[] b = decoder.decodeBuffer(base64str);for (int i = 0; i < b.length; ++i) { //调整异常数据if (b[i] < 0) {b[i] += 256;}}outFileStream(b);return true;}
生成图片
public static void outFileStream(byte[] b) throws IOException {//生成jpeg图片String date = "MMdd-HH:mm:ss";SimpleDateFormat time = new SimpleDateFormat(date);Date data = new Date();String timeName=time.format(data);String filename = savePath + timeName + ".png";OutputStream out = Files.newOutputStream(Paths.get(filename));out.write(b);out.flush();out.close();}