Base64編碼圖片存取與前台顯示


需求:將Base64編碼圖片以BLOB類型存入數據庫,需要時取出顯示

后台:

String base64str=new String(log.getRequest_imgdata());//log為實體 括號里面是圖像的get方法 返回為Byte[]型
String new str=new String("\"data:image/jpg;base64,"+base64str+"\"");//拼裝Base64字符串頭
response.getWriter().write(newstr);//將完整Base64字符串返回前台

前台Js:

var srcUrl = appJP.urlReqImg +"?log_id="+row.log_id;//請求URL
        $.get(srcUrl,function(data){
            var imgWindow = $("#imgDetail").html("<img src="+data+">");//接收Base64字符串,並轉換為圖片顯示
            $("#showImg").window({title:"圖片詳情",width:"auto"}).window("open").window("center");
        })

 

以上已實現從數據庫取出BLOB類型Base64圖像數據(Java中為byte[])轉換為字符串,並發送至前台顯示

但是在測試中發現稍微大一點的圖像(幾百KB)在部分IE瀏覽器中不能顯示,查詢資料發現是IE8以下對Base64解碼長度限制的問題

 

解決方案:更換后台到前台傳輸圖像數據形式為流的形式

后台:  

String base64str=new String(log.getRequest_imgdata());
BASE64Decoder decoder=new BASE64Decoder();
byte[] imgbyte=decoder.decodeBuffer(base64str);//解碼Base64圖片數據
response.setContentType("image/jpeg");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(imgbyte);
outputStream.flush();

前台js:

var srcUrl = appJP.urlReqImg +"?log_id="+row.log_id;
var imgWindow = $("#imgDetail").html("<img src="+srcUrl+">");
$("#showImg").window({title:"圖片詳情",width:"auto"}).window("open").window("center");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM