Java基于Base64實(shí)現(xiàn)編碼解碼圖片文件
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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. vue style width a href動(dòng)態(tài)拼接問題的解決2. Java源碼解析之接口List3. 在vue中獲取wangeditor的html和text的操作4. python mysql 字段與關(guān)鍵字沖突的解決方式5. Python用K-means聚類算法進(jìn)行客戶分群的實(shí)現(xiàn)6. Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作7. python編寫五子棋游戲8. 解決Android Studio Design界面不顯示layout控件的問題9. 使用vue-cli創(chuàng)建項(xiàng)目并webpack打包的操作方法10. python讀取中文路徑時(shí)出錯(cuò)(2種解決方案)
