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.后臺(tái)
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ù)庫,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選