プロのテスト利用可能:
コード:
1 インポートsun.misc.BASE64Decoder; 2 インポートsun.misc.BASE64Encoder; 3 4。 インポート java.io. * ; 5 。6つの パブリック クラスBase64Util { 7。 8。 / * BASE64符号化された画像を変換する* / 9。 パブリック 静的 ブールgenerateImage(文字列imgStr、文字列のパス){ 10 IF(imgStr == NULL ) 11。 復帰 はfalse ; 12である Base64Decoderデコーダ= 新しい新しいBase64Decoder(); 13である トライ{ 14 バイト [] B = decoder.decodeBuffer(imgStr)。 15 のためには、(int型 i = 0; iは<てb.length; iは++ ){ 16 であれば(B [i]が<0 ){ 17件の B [I] + = 256 。 18 } 19 } 20 のOutputStreamアウト= 新しいたFileOutputStream(パス)。 21 out.write(B) 22 out.flush()。 23 out.close()。 24 リターン 真。 25 } キャッチ(例外E){ 26は e.printStackTrace(); 27 リターン falseに; 28 } 29 } 30 31である * / ピクチャがbase64エンコードに変換される* / 32 パブリック 静的文字列getImageStr(文字列imgfile){ 33は InputStream InputStreamは= NULL ; 34である バイト []データ= NULL ; 35 試み{ 36 のinputStream = 新しい新規のFileInputStream(imgfile); 37 データ= 新しい新しい バイト[inputStream.available()]; 38は 、InputStream.read(データ) 39 inputStream.close(); 40 } キャッチ(IOExceptionをE){ 41は e.printStackTrace(); 42れる } 43である Base64Encoderエンコーダ= 新しい新しいBase64Encoder(); 44れます 戻りencoder.encode(データ); 45 } 46がある * / これは、上記の方法の使用であり、無視することができる* / 47 / * パブリック静的無効メイン(文字列[] args){ 48 * // * base64エンコードを変換する画像* * // 49 ストリングstrImg = getImageStr( "画像転送パス:C:/ユーザー/画像.PNG"); 50 のSystem.out.println(strImg); 51である * // * BASE64符号化画像に変換する* // * 52である generateImage (strImg、strImg * // * strImgここでBASE64符号化される* @ * ); 53 } * / 54 }