当前位置: 移动技术网 > IT编程>开发语言>Java > Struts2中图片以base64方式上传至数据库

Struts2中图片以base64方式上传至数据库

2019年07月22日  | 移动技术网IT编程  | 我要评论

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方式上传至数据库,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网