日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

SpringMVC处理MYSQL BLOB字段的上传

發布時間:2025/3/20 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC处理MYSQL BLOB字段的上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

任務:

uos.docfile的content字段是longblob類型的,通過頁面將文件存儲到這個字段里。

頁面代碼

<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中代碼,這部分只是個中轉

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中代碼,這部分是實質性代碼

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();}}});}

?














本文轉自張昺華-sky博客園博客,原文鏈接:http://www.cnblogs.com/xiandedanteng/p/4168882.html,如需轉載請自行聯系原作者

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。