Struts2中圖片以base64方式上傳至數(shù)據(jù)庫
更新時間:2016年09月30日 11:50:44 作者:Alexdevlin
這篇文章主要介紹了Struts2中圖片以base64方式上傳至數(shù)據(jù)庫的實現(xiàn)代碼,代碼分為前臺和后臺兩段,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
1.頁面 這里輸入代碼
<div> <span id="uploadImg" style="margin:50px;background-color:#ddd;display:inline-block;height:130px;width:200px;"> <span style="color:#bbb;font-weight:600;border:2px #ccc dashed;font-size:20px;text-align:center;display:inline-block;height:50px;width:50px;line-height:50px;position:absolute;margin-top:40px;margin-left:75px;z-index:99">+ </span> <img id="preview" style="display: none; "> </span> <input type="file" style="display:none" name="ImgCard" id="imgFileBtn" id="imgFileBtn" style="width:150px;" onchange="javascript:setImagePreview();"/> </div>
2.后臺
private File ImgCard;
private String ImgCardContentType;
private String ImgCardFileName;
public void getImg(){
BASE64Encoder encoder = new BASE64Encoder();
BufferedImage bi;
boolean isImage = false;
String[] imgExts = {".jpg", ".jpeg",".bmp", ".png"};
for(String ext : imgExts) {
if(ImgCardFileName.toLowerCase().endsWith(ext)) {
isImage = true;
break;
}
}
if((ImgCard.length()/1024/1024)>3){
return ERROR;
}
bi = ImageIO.read(ImgCard);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
String img= encoder.encodeBuffer(bytes).trim();
}
以上所述是小編給大家介紹的Struts2中圖片以base64方式上傳至數(shù)據(jù)庫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中transient關(guān)鍵字的詳細總結(jié)
本文要介紹的是Java中的transient關(guān)鍵字,transient是短暫的意思。對于transient 修飾的成員變量,在類的實例對象的序列化處理過程中會被忽略,感興趣的朋友可以參考閱讀2023-04-04
Java生產(chǎn)1-100的隨機數(shù)簡單實例(分享)
下面小編就為大家?guī)硪黄狫ava生產(chǎn)1-100的隨機數(shù)簡單實例(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
使用Spring處理x-www-form-urlencoded方式
這篇文章主要介紹了使用Spring處理x-www-form-urlencoded方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

