zoukankan      html  css  js  c++  java
  • 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();
                            }
                         }
                });
        }
    
    
  • 相关阅读:
    Jupsh_flutter Android 收不到消息 / 排查不出请根据第 9 点说明提供信息
    angular表单 Dom获取表单值以及双向数据绑定
    angular中的组件以及组件中的模板合成
    Flutter开发的app进行设备判断是Ios还是android
    Java的封装
    GCD编程 之 略微提高篇
    多线程基础(六)GCD基础
    我遇到的CocoaPods的问题(也许后期会解决,持续更新)
    iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
    多线程基础(五)NSThread线程通信
  • 原文地址:https://www.cnblogs.com/heyang78/p/4168882.html
Copyright © 2011-2022 走看看