开发者社区> 桃子红了呐> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
MySQL:json字段查询:数组、对象、成员检查
MySQL:json字段查询:数组、对象、成员检查
16 0
【Python】【MySQL】Python将JSON数据以文本形式存放到MySQL的Text类型字段中
【Python】【MySQL】Python将JSON数据以文本形式存放到MySQL的Text类型字段中
28 0
mysql varchar类型字段为数字时,不带引号查询时查询结果与事实不符
mysql varchar类型字段为数字时,不带引号查询时查询结果与事实不符
60 0
mysql字符串等值查询中条件字段值末尾有空格也能查到数据问题
mysql字符串等值查询中条件字段值末尾有空格也能查到数据问题
41 0
Mysql中查询数据库中包含某个字段的所有表名
Mysql中查询数据库中包含某个字段的所有表名
37 0
mysql将字符串类型字段后面的小数点和零去掉
mysql将字符串类型字段后面的小数点和零去掉
41 0
mysql sum函数中对两字段做运算时有null时的情况
mysql sum函数中对两字段做运算时有null时的情况
30 0
Mysql中通过关联update将一张表的一个字段更新到另外一张表中
Mysql中通过关联update将一张表的一个字段更新到另外一张表中
66 0
mysql 进行update时,要更新的字段中有单引号或者双引号导致不能批量生成sql的问题...
mysql 进行update时,要更新的字段中有单引号或者双引号导致不能批量生成sql的问题...
42 0
MySQL:递减/递减更新一列字段值
MySQL:递减/递减更新一列字段值
33 0
+关注
桃子红了呐
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
高效MySQL的N个习惯
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关镜像