• 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();
                            }
                         }
                });
        }
    
    
  • 相关阅读:
    Class文件和JVM的恩怨情仇
    详解及对比创建线程的三种方式
    浅析Java中线程组(ThreadGroup类)
    简单定义多线程!
    五分钟看懂UML类图与类的关系详解
    LeetCode刷题--14.最长公共前缀(简单)
    LeetCode刷题--13.罗马数字转整数(简答)
    动态规划算法详解及经典例题
    LeetCode--9.回文数(简单)
    LeetCode刷题--7.整数反转(简单)
  • 原文地址:https://www.cnblogs.com/heyang78/p/4168882.html
Copyright © 2020-2023  润新知