1. 页面页面
<div> <span id = "uploadimg" style = "มาร์จิ้น: 50px; พื้นหลัง-สี: #ddd; แสดง: inline-block; ความสูง: 130px; ความกว้าง: 200px;"> <span style = "สี: #bbb; font-weight: 600; border: 2px #ccc Dashed; Font-Size: 20px; Text-Align: Center; Display: Inline-block; ความสูง: 50px; ความกว้าง: 50px; จุดสูง: 50px; ตำแหน่ง: สัมบูรณ์; style = "display: none" name = "imgcard" id = "imgFileBtn" id = "imgFileBtn" onChange = "JavaScript: setImagePreview ();"/> </div>
2. 后台
ไฟล์ส่วนตัว imgcard; สตริงส่วนตัว imgcardcontentType; สตริงส่วนตัว imgcardfilename; โมฆะสาธารณะ getimg () {base64Encoder encoder = ใหม่ base64Encoder (); bufferedimage bi; boolean isimage = false; String [] imgexts = {".jpg", ".jpeg", ". bmp", ".png"}; สำหรับ (String ext: imgexts) {ถ้า (imgcardfilename.toLowerCase (). endswith (ext)) {isImage = true; หยุดพัก; }} if ((imgcard.length ()/1024/1024)> 3) {return error;} bi = imageio.read (imgcard); ByTeArrayOutputStream BAOS = New ByTeArrayOutputStream (); Imageio.write (bi, "jpg", baos); ไบต์ [] bytes = baos.tobytearray (); string img = encoder.encodeBuffer (ไบต์) .Trim (); -以上所述是小编给大家介绍的 struts2 中图片以 base64 方式上传至数据库, 希望对大家有所帮助, 如果大家有任何疑问请给我留言,, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!