Convert base64 encoded string to image and write to file

public static String base64StrToImage(String imgStr) {         String path = "D:\\appimg2\\imgshi\\";         //Remove base64 prefix         String newStr = imgStr.replaceAll("data:image/png;base64,", "" );         // Image classification path + image name + image suffix         String string22 = UUID.randomUUID().toString();         String imgClassPath = path.concat(string22).concat(".jpg");         if (imgStr == null )         return "Upload failed";         BASE64Decoder decoder = new BASE64Decoder();         try {                 // Decrypt                 byte[] b = decoder.decodeBuffer(newStr);                 // Process data                 for (int i = 0; i < b.length; + +i) {














                        if (b[i] < 0) {                 b[i] += 256;                 }         }         //If the folder does not exist, it will automatically create         File tempFile = new File(imgClassPath);         if (!tempFile.getParentFile().exists() ) {                 tempFile.getParentFile().mkdirs();         }         OutputStream out = new FileOutputStream(tempFile);         out.write(b);         out.flush();         out.close();         //Get local ip         String hostAddress = InetAddress .getLocalHost().getHostAddress();         String urls = hostAddress +":9090/bigscreen/tempImages/"+string22+".jpg";         return urls;
















        } catch (Exception e) {         return "Upload failed";         } }


Guess you like

Origin blog.csdn.net/weixin_60415789/article/details/132805773