任务:

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

SpringMVC处理MYSQL BLOB字段的上传的更多相关文章

  1. SpringMVC处理MySQL BLOB字段的下载

    任务: uos.docfile的content字段是longblob类型,通过Web点击链接能下载到存储在这个字段里的文件.Web点击链接类似如下形式: http://localhost:8080/d ...

  2. SpringMVC结合ajaxfileupload文件无刷新上传

    jQuery没有提供ajax的文件上传,我们可以通过ajaxfileupload实现ajax文件的上传.其实ajaxfileupload文件上传特别的简单.下面就演示一下在SpringMVC中实现aj ...

  3. spring--mvc添加用户及用户头像上传

    spring--mvc添加用户及用户头像上传 添加用户步骤: 1.用ajax获取省份信息 2.添加用户 代码:register.jsp <meta http-equiv="Conten ...

  4. JavaScript把项目本地的图片或者图片的绝对路径转为base64字符串、blob对象在上传

    主题: JavaScript把项目本地的图片或者图片的绝对路径转为base64字符串.blob对象在上传. 用处: 从本地选择图片上传,如项目规定只能选择本项目文件夹下的图像上传为头像等. 主要思想: ...

  5. 阿里云CentOS自动备份MySql 8.0并上传至七牛云

    本文主要介绍一下阿里云CentOS7下如何对MySql 8.0数据库进行自动备份,并使用.NET Core 将备份文件上传至七牛云存储上,并对整个过程所踩的坑加以记录. 环境.工具.准备工作 服务器: ...

  6. SpringMVC(四)-- springmvc的系统学习之文件上传、ajax&json处理

    资源:尚学堂 邹波 springmvc框架视频 一.文件上传 1.步骤: (1)导入jar包 commons-fileupload,commons-io (2)在springmvc的配置文件中配置解析 ...

  7. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  8. js bese64转化为blob使用FormData上传

    原文 工作示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  9. SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传

    1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...

随机推荐

  1. linxu安装方式

    安装Linux操作系统的5种方法以及心得这几天没有调别的东西,想起自己还不太会在没有安装光盘的时候安装Linux,于是试了一下Linux的五种安装方法,下面是我的一些 篇一:安装Linux操作系统的5 ...

  2. 51Nod 1561 另一种括号序列

    题目链接 分析: 卡常数~~~好气啊~~~这是看脸的时代啊~~~ $A$代表$($的数量,$B$代表$)$的数量... 如果$($的数量多于$)$的数量,那么最有方案显然是添加$A-B$个$)$... ...

  3. 第2章 掌握C++

    参考: https://blog.csdn.net/u014162133/article/details/46573873 1.C++主要特点: 封装性(Encapsulation):把数据与操作数据 ...

  4. Linux系统调用--getrlimit()与setrlimit()函数详解【转】

    转自:http://www.cnblogs.com/niocai/archive/2012/04/01/2428128.html 功能描述:获取或设定资源使用限制.每种资源都有相关的软硬限制,软限制是 ...

  5. linux系统调用实现代码分析【转】

    转自:http://linux.chinaunix.net/doc/kernel/2001-07-30/637.shtml 启动早就读完,现在为了写笔记再从启动之后粗略的大体读一遍,基本就是几个大模块 ...

  6. (七)ubuntu下编译openwrt内核的环境配置

    首先安装基本开发环境: sudo apt-get install ssh vim samba tftp nfs 安装编译openwrt须要的包: 解压openwrt包编译出错: Build depen ...

  7. springBoot【01】

    /* 使用spring官网的 http://start.spring.io/ 来建立项目包 生成入口文件,入口文件中对类注释@SpringBootApplication,这个注释是唯一的,标明这个类是 ...

  8. 使用dom4j工具包对xml文件解析

    xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http: ...

  9. 使用Vagrant打造Linux开发环境

    1.安装Oracle VM VirtualBox.vagrant,安装完成需重启电脑 Oracle VM VirtualBox安装包下载:https://www.virtualbox.org/wiki ...

  10. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...