SpringMVC处理MYSQL BLOB字段的上传-阿里云开发者社区

开发者社区> 数据库> 正文

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,如需转载请自行联系原作者

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

其他文章