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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

SpringMVC处理MYSQL BLOB字段的上传

發(fā)布時(shí)間:2025/3/20 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC处理MYSQL BLOB字段的上传 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

任務(wù):

uos.docfile的content字段是longblob類型的,通過(guò)頁(yè)面將文件存儲(chǔ)到這個(gè)字段里。

頁(yè)面代碼

<div class="box"><div class="box-head"><h2>Upload a document</h2></div><form name="form1" action="uploadDocument.html" method="post" ENCTYPE="multipart/form-data"><div class="form" ><p><span class="req"><input id="remarkTxt" name="remarkTxt" class="field size4" title="Enter the date" /></span><label>Remark: <span>(The brief introduction of the file)</span></label></p><p><span class="req"><input type="file" name="uploadFileCtrl" class="field size4" title="Choose the file" /></span><label>Upload file: <span>(Max Size:20M)</span></label></p></div><div class="buttons"><input id="queryBtn" type="button" class="button" value="Submit" /></div></form> </div>

?

1.控制器的代碼

@RequestMapping(value="/uploadDocument")public String uploadDocument(@RequestParam("remarkTxt") String remark, @RequestParam("uploadFileCtrl") MultipartFile file,HttpServletRequest request,HttpServletResponse response){try {// 從session中獲得用戶String userId=getUserIdFromSession(request);// 得到上傳文件名String uploadFileName=file.getOriginalFilename();request.setAttribute("uploadFileName", uploadFileName);if(file.isEmpty()==false){InputStream is=file.getInputStream();service.uploadDocument(remark, is,file.getSize(),userId,uploadFileName);is.close();return "/pages/doc/result/index.jsp";}else{throw new Exception("The file you uploaded is NULL. Please check and retry.");}} catch (Exception e) {e.printStackTrace();logger.error(e);request.setAttribute("error", e.getClass());request.setAttribute("reason", e.getMessage());StackTraceElement[] arr=e.getStackTrace();request.setAttribute("stackTraceElements", arr);return "pages/error/index.jsp";}}

2.Serivce中代碼,這部分只是個(gè)中轉(zhuǎn)

public int uploadDocument(String remark,InputStream fin,long filesize,String email,String uploadFileName) throws Exception{return getPosDao().uploadDocument(remark, fin,filesize,email,uploadFileName);}

3.DAO中代碼,這部分是實(shí)質(zhì)性代碼

public int uploadDocument(final String remark,final InputStream fin,final long filesize,final String email,String uploadFileName) throws Exception{final LobHandler lobHandler=new DefaultLobHandler();return this.getJdbcTemplate().execute("insert into uos.docfile(remark,content,email,addtime,filename) values ('"+remark+"',?,'"+email+"',NOW(),'"+uploadFileName+"' )",new AbstractLobCreatingPreparedStatementCallback(lobHandler){ protected void setValues(PreparedStatement pstmt,LobCreator lobCreator){try {lobCreator.setBlobAsBinaryStream(pstmt,1,fin,(int)filesize);} catch (SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}}});}

?














本文轉(zhuǎn)自張昺華-sky博客園博客,原文鏈接:http://www.cnblogs.com/xiandedanteng/p/4168882.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者

與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的SpringMVC处理MYSQL BLOB字段的上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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