日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java将图片保存进mysql_Java存储图片到Mysql

發(fā)布時間:2023/12/15 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java将图片保存进mysql_Java存储图片到Mysql 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓

【1】視圖層

action="${ctx}/web/UserInforServlet?method=userInforServlet" >

更換頭像

立即提交

重置

var layer,upload,form;

//1-頁面數(shù)據(jù)加載

$(function () {

//【1】加載&初始化layui模塊-彈出層與table數(shù)據(jù)表格

layui.use(["layer", "upload","form"], function () {

layer = layui.layer;

laydate=layui.laydate;

upload = layui.upload;//layui的上傳

form= layui.form;

uploadImage();

});

});

function uploadImage(){

var $ = layui.jquery;

upload.render({

elem: "#upImage"

,auto: false

,size:1024

,choose: function(obj){//使用choose選擇文件后的回調(diào)函數(shù)

//預(yù)讀本地文件示例,不支持ie8

obj.preview(function(index, file, result){

$("#userImge").attr("src",result); //圖片鏈接(base64)

});

}

});

}

//提交form表單

$("#saveUserInfor").click(function(){

$("#frregister").ajaxSubmit(function(data) {

if(data="1"){

layer.msg("成功!");

}

});

});

【2】控制器

publicvoid userInforServlet(HttpServletRequest request,HttpServletResponseresponse) throws IOException {

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=UTF-8");//處理響應(yīng)編碼

Map map=new HashMap();

UserDetail userDetail=new UserDetail();

try {

//【1】解析數(shù)據(jù)

DiskFileItemFactory fac=new DiskFileItemFactory();

int sizeThreshold=1024*1024*10; //10MB

fac.setSizeThreshold(sizeThreshold);

fac.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload=new ServletFileUpload(fac);//文件上傳解析器

upload.setHeaderEncoding("utf-8");

Listlist=upload.parseRequest(request);

//【2】遍歷集合

for(FileItem item :list){

//1)如果當前FileItem對象是普通項

if(item.isFormField()){

map.put(item.getFieldName(),item.getString());

}else{

//1)如果當前FileItem對象是上傳項

//2)判斷是否存在有該文件夾

String realPath=("D:/Mytouimage");

File uploadMkdir=new File(realPath);

if(!uploadMkdir.exists()){

uploadMkdir.mkdir();

}

//如果fileitem中封裝的是上傳文件,得到上傳的文件名稱,

String fileName = item.getName();//上傳文件的名

//多個文件上傳時,沒有上傳內(nèi)容的問題異常處理

if(fileName==null||"".equals(fileName.trim())){ continue;

}

fileName =fileName.substring(fileName.lastIndexOf("\\")+1);

InputStream is=item.getInputStream();

File file=new File(realPath+"/"+fileName);

//3)先在本地存儲圖片,再讀取保存到數(shù)據(jù)庫

if(!file.isDirectory()){

userDetail.setImageName(realPath+"/"+item.getName());

OutputStream os=new FileOutputStream(file);

//IOUtils拷貝流

IOUtils.copy(is,os);

//關(guān)閉資源

IOUtils.closeQuietly(is);

IOUtils.closeQuietly(os);

item.delete();//刪除處理文件上傳時生成的臨時文件

}

}

}

BeanUtils.populate(userDetail,map);

int flag =ids.insert(userDetail);

if(flag==1){

response.getWriter().write("1");

response.getWriter().close();

}

}

【3】dao層

@Override

publicint insert(UserDetail t) {

int flag=0;

InputStream fis=null;

String insert="INSERT INTOr_userdetail(userImage,imageName)" +

"VALUES(?,?);";

try {

con=JDBCUtil.getConnection();

ps=con.prepareStatement(insert);

if(t.getImageName()!=null){

//讀取本地圖片

fis=new FileInputStream(t.getImageName());

}

//將流寫入Mysql

ps.setBinaryStream(1, fis, fis.available());

ps.setString(2, t.getImageName());

flag=ps.executeUpdate();

} catch (SQLException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

catch (FileNotFoundException e){

//TODO Auto-generated catch block

e.printStackTrace();

}

catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

return flag;

}

【4】po層

publicclass UserDetail implements Serializable{

publicstaticfinallongserialVersionUID = 42L;

privateintuserDetailID;

private Blob userImage;

private String imageName;

public Blob getUserImage() {

returnuserImage;

}

publicvoid setUserImage(Blob userImage) {

this.userImage = userImage;

}

public String getImageName() {

returnimageName;

}

publicvoid setImageName(String imageName) {

this.imageName = imageName;

}

}

【5】總結(jié)

1)注意上傳圖片是form表單一定要加method="post" enctype="multipart/form-data" ,否則無法將數(shù)據(jù)上傳到服務(wù)器。

2)Mysql存儲圖片的數(shù)據(jù)類型

TinyBlob最大 255

Blob 最大 65K

MediumBlob 最大 16M

LongBlob 最大 4G

3)在dao層向數(shù)據(jù)庫添加數(shù)據(jù)

使用ps.setBinaryStream(1,fis, fis.available());

【6】顯示圖片

publicvoid selectUserImage(HttpServletRequest request, HttpServletResponseresponse) {

String id=request.getParameter("userID");

if(id!=null){

int userID=Integer.parseInt(id);

UserDetail userDetail=ids.findByUserid(userID);

BufferedOutputStream bufos=null;

BufferedInputStream bufis=null;;

try {

if(userDetail.getUserImage()!=null){

//讀取緩沖區(qū) userDetail.getUserImage().getBinaryStream()--讀取源

bufis=new BufferedInputStream(userDetail.getUserImage().getBinaryStream());

//輸出緩沖區(qū) response.getOutputStream()--輸出目的

bufos = new BufferedOutputStream(response.getOutputStream());

byte[] bt=newbyte[1024];

//【6】初始化實際長度的數(shù)組的長度

int count=0;

//【7】循環(huán)讀取多媒體文件數(shù)據(jù),將其讀取的數(shù)據(jù)存儲在byte數(shù)組

while((count=bufis.read(bt))!=-1){

//【8】截取數(shù)組從零到實際長度,循環(huán)寫入多媒體的目標文件

bufos.write(bt,0,count);

}

}

bufis.close();

bufos.close();

} catch (SQLException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java将图片保存进mysql_Java存储图片到Mysql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。