欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件

 更新時(shí)間:2020年03月30日 09:42:45   作者:NemoWang  
這篇文章主要介紹了Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

BASE64 編碼是一種常用的字符編碼,在很多地方都會(huì)用到。但base64不是安全領(lǐng)域下的加密解密算法。能起到安全作用的效果很差,而且很容易破解,他核心作用應(yīng)該是傳輸數(shù)據(jù)的正確性,有些網(wǎng)關(guān)或系統(tǒng)只能使用ASCII字符。Base64就是用來將非ASCII字符的數(shù)據(jù)轉(zhuǎn)換成ASCII字符的一種方法,而且base64特別適合在http,mime協(xié)議下快速傳輸數(shù)據(jù)。

1、編碼與解碼代碼如下所示:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
 
import javax.imageio.ImageIO;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
/**
 * @author zxn
 * @version 創(chuàng)建時(shí)間:2014-7-2 上午11:40:40
 * 
 */
public class ImageUtils {
  /**
   * 將網(wǎng)絡(luò)圖片進(jìn)行Base64位編碼
   * 
   * @param imgUrl
   *      圖片的url路徑,如http://.....xx.jpg
   * @return
   */
  public static String encodeImgageToBase64(URL imageUrl) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
    ByteArrayOutputStream outputStream = null;
    try {
      BufferedImage bufferedImage = ImageIO.read(imageUrl);
      outputStream = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "jpg", outputStream);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 對(duì)字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
  }
 
  /**
   * 將本地圖片進(jìn)行Base64位編碼
   * 
   * @param imgUrl
   *      圖片的url路徑,如http://.....xx.jpg
   * @return
   */
  public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
    ByteArrayOutputStream outputStream = null;
    try {
      BufferedImage bufferedImage = ImageIO.read(imageFile);
      outputStream = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "jpg", outputStream);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 對(duì)字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
  }
 
  /**
   * 將Base64位編碼的圖片進(jìn)行解碼,并保存到指定目錄
   * 
   * @param base64
   *      base64編碼的圖片信息
   * @return
   */
  public static void decodeBase64ToImage(String base64, String path,
      String imgName) {
    BASE64Decoder decoder = new BASE64Decoder();
    try {
      FileOutputStream write = new FileOutputStream(new File(path
          + imgName));
      byte[] decoderBytes = decoder.decodeBuffer(base64);
      write.write(decoderBytes);
      write.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

2、直接在頁面上顯示base64編碼的圖片

 <html>
 <body>
 <img src='data:image/jpg;base64,base64碼'/> 
 </body>
 </html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論