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

 


posted @ 2014-12-17 10:56  逆火狂飙  阅读(702)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东