Image Java vers la chaîne Base64, chaîne de base64 à l'image, le contenu spécifique est le suivant
1. Image à la chaîne de base64:
/ ** * Base64-Coded String to Image * @param imgstr Base64-Encoded String * @param path path path * @return * / public static boolean base64strtoimage (string imgstr, string path) {if (imgstr == null) return false; Base64DECODER DECODER = NOUVEAU BASE64DECODER (); essayez {// decrypt byte [] b = decoder.decodeBuffer (imgstr); // Processus des données pour (int i = 0; i <b.length; ++ i) {if (b [i] <0) {b [i] + = 256; }} // Le fichier est automatiquement créé si le dossier n'existe pas. tempFile = nouveau fichier (chemin); if (! tempfile.getParentfile (). existant ()) {tempfile.getParentfile (). mkDirs (); } OutputStream out = new FileOutputStream (tempfile); out.write (b); out.flush (); out.close (); Retour Vrai; } catch (exception e) {return false; }}2. String Base64 à l'image:
/ ** * Image à base64 String * @param iMgFile Image Path * @return * / public static string imageToBase64Str (String imgfile) {inputStream inputStream = null; octet [] data = null; try {inputStream = new FileInputStream (imgFile); data = nouveau octet [inputStream.Available ()]; inputStream.read (data); inputStream.close (); } catch (ioException e) {e.printStackTrace (); } // Encoper Base64Encoder Encoder = new Base64Encoder (); return Encoder.encode (données); }3. Test:
public static void main (String [] args) {String Base64Str = ImageToBase64Str ("d: /pic/001.jpg"); System.out.println (Base64Str); Boolean B = Base64strtoimage (Base64Str, "d: /pic/temp/002.jpg"); System.out.println (b); }Image de reproduction:
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.