Conversion between Java files and Base64
1. File to Base64 tool class
You can convert pictures and videos into Base64 format
/** * File to Base64 * @param filePath * @return */ public static String convertFileToBase64(String filePath) { try { // Read the file as a byte array byte[] fileBytes = ((filePath)); // Convert byte array to Base64 encoded string String base64EncodedString = ().encodeToString(fileBytes); return base64EncodedString; } catch (Exception e) { (); return null; } }
2. Base64 file conversion tool class
Download Base64 format pictures and videos to local
/** * Base64 to file * @param base64String Base64 string * @param filePath The output file path * @param mimeType * MIME type: * Video video/mp4 * PNG: image/png * JPEG: image/jpeg * GIF: image/gif * BMP: image/bmp * WebP: image/webp * @return */ public static boolean convertBase64ToFile(String base64String, String filePath, String mimeType) { try { // Convert Base64 encoded string to byte array byte[] fileBytes = ().decode(base64String); // Create file header information String header = "data:" + mimeType + ";base64,"; byte[] headerBytes = (); // Merge file header and file content byte[] combinedBytes = new byte[ + ]; (headerBytes, 0, combinedBytes, 0, ); (fileBytes, 0, combinedBytes, , ); // Write byte array to file ((filePath), fileBytes); return true; } catch (Exception e) { (); return false; } }
3. Comprehensive case
package ; import ; import ; import .*; public class FileToBase64Converter { /** * File to Base64 * @param filePath * @return */ public static String convertFileToBase64(String filePath) { try { // Read the file as a byte array byte[] fileBytes = ((filePath)); // Convert byte array to Base64 encoded string String base64EncodedString = ().encodeToString(fileBytes); return base64EncodedString; } catch (Exception e) { (); return null; } } /** * File to Base64 process */ public static List<Map<String, String>> fileToBase64() { List<Map<String, String>> dataList = new ArrayList<>(); // The image path to be read String filePath = "D:\\repo\\java_base_test\\static\\img\\"; // Read video path String videoPath = "D:\\repo\\java_base_test\\static\\video\\cs.mp4"; String fileToBase64 = convertFileToBase64(filePath); String videoToBase64 = convertFileToBase64(videoPath); if (fileToBase64 != null) { ("Picture conversion is successful"); (new HashMap<String, String>() {{ put("outPath", ("D:\\repo\\java_base_test\\static\\img\\GcJcSbJkBjVo_%", new Date().getTime())); put("base64Str", fileToBase64); put("mimeType", "image/png"); }}); } else { ("Picture conversion failed"); } if (videoToBase64 != null) { ("Video conversion is successful"); (new HashMap<String, String>() {{ put("outPath", ("D:\\repo\\java_base_test\\static\\video\\cs_%s.mp4", new Date().getTime())); put("base64Str", videoToBase64); put("mimeType", "video/mp4"); }}); } else { ("Video conversion failed"); } return dataList; } /** * Base64 to file * @param base64String Base64 string * @param filePath The output file path * @param mimeType * MIME type: * Video video/mp4 * PNG: image/png * JPEG: image/jpeg * GIF: image/gif * BMP: image/bmp * WebP: image/webp * @return */ public static boolean convertBase64ToFile(String base64String, String filePath, String mimeType) { try { // Convert Base64 encoded string to byte array byte[] fileBytes = ().decode(base64String); // Create file header information String header = "data:" + mimeType + ";base64,"; byte[] headerBytes = (); // Merge file header and file content byte[] combinedBytes = new byte[ + ]; (headerBytes, 0, combinedBytes, 0, ); (fileBytes, 0, combinedBytes, , ); // Write byte array to file ((filePath), fileBytes); return true; } catch (Exception e) { (); return false; } } /** * Base64 file conversion process * @param base64String * @param filePath */ public static void base64ToFile(List<Map<String, String>> dataList) { for (Map<String, String> resMap : dataList) { boolean flag = convertBase64ToFile(("base64Str"), ("outPath"), ("mimeType")); if (flag) { (("outPath") + "Conversion successful"); } else { (("outPath") + "Conversion failed"); } } } public static void main(String[] args) { // File to Base64 List<Map<String, String>> dataList = fileToBase64(); // Base64 to file base64ToFile(dataList); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.