1. 页面 这里输入代码
<div> <span id = "uploadImg" style = "margem: 50px; colorido de fundo: #ddd; display: em linha-bloco; altura: 130px; largura: 200px;"> <span style = "cor: #bbb; font-weight: 600; border: 2px #ccc Traced; font-size: 20px; alinhamento de texto: centro; exibição: bloco em linha; altura: 50px; largura: 50px; altura da linha: 50px; posição: absoluto; margem-top: 40px; margin-left: 75px; z-index: 99 ">+</span> <Img id =" vislic) " style = "display: nenhum" name = "imgcard" id = "imgfilebtn" id = "imgfileBtn" onchange = "javascript: setImagePreview ();"/> </div>
2. 后台
arquivo privado imgcard; string privada imgcardContentType; string privada imgcardfilename; public void getImg () {base64Encoder coder = new base64Encoder (); Bufedimage bi; boolean isimage = false; String [] imgexts = {".jpg", ".jpeg", ". Bmp", ".png"}; for (string ext: imgexts) {if (imgcardfilename.tolowercase (). endswith (ext)) {iSimage = true; quebrar; }} 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 = coder.encodeBuffer (bytes) .Trim (); }以上所述是小编给大家介绍的 struts2 中图片以 base64 方式上传至数据库 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!