1. 页面 这里输入代码
<Div> <span id = "unggah" style = "margin: 50px; latar belakang-warna: #ddd; display: inline-block; tinggi: 130px; lebar: 200px;"> <span style = "color: #bbb; font-weight: 600; perbatasan: 2px #ccc putus-putus; font-size: 20px; text-align: center; display: inline-block; tinggi: 50px; lebar: 50px; line-height: 50px; posisi: absolute; margin-top: 40px; margin-left: 75px; z-index: 99 "> </span> <Img id =" preview "; style = "display: none" name = "imgcard" id = "imgfilebtn" id = "imgfileBtn" onchange = "javascript: setimagePreview ();"/> </div>
2. 后台
file private imgcard; string pribadi imgcardcontenttype; private string imgcardfileName; public void getImg () {base64Encoder encoder = base64encoder baru (); BufferedImage Bi; boolean isimage = false; String [] imgexts = {".jpg", ".jpeg", ". Bmp", ".png"}; untuk (string ext: imgexts) {if (imgcardfileName.tolowercase (). endswith (ext)) {isImage = true; merusak; }} if ((imgcard.length ()/1024/1024)> 3) {return error;} bi = Imageo.read (imgcard); BytearrayoutputStream baos = new bytearrayoutputStream (); Imageo.write (bi, "jpg", Baos); byte [] bytes = Baos.tobyteArray (); String img = encoder.encodeBuffer (bytes) .trim (); }以上所述是小编给大家介绍的 struts2 中图片以 base64 方式上传至数据库 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!