SpringMVC处理MYSQL BLOB字段的上传
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC处理MYSQL BLOB字段的上传
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
任務(wù):
uos.docfile的content字段是longblob類型的,通過頁面將文件存儲(chǔ)到這個(gè)字段里。
頁面代碼:
<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ù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的SpringMVC处理MYSQL BLOB字段的上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超好用的简单的jquery tab选项卡
- 下一篇: Lync 小技巧-48-Lync 语音邮