Spring MVC返回BLOB类型的图片
来源:互联网 发布:英语教师网络研修心得 编辑:程序博客网 时间:2024/06/08 16:32
项目中有个表专门用来存用户的头像,以BLOB类型存储,然后通过mybatis读取数据,通过Spring MVC以流的形式返回到前台去:
后端代码:
package com.gz.medicine.yun.common.controller;import com.gz.medicine.common.util.ValidateWithException;import com.gz.medicine.yun.common.bean.FromBlob;import com.gz.medicine.yun.common.mapper.FromBlobMapper;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;/** * @version V1.0 * @Author fendo * @ClassName ImagesController * @PackageName com.gz.medicine.yun.common.controller * @Description 图片 controller * @Data 2017-08-21 10:08 **/@RequestMapping("images")@Controllerpublic class ImagesController extends ValidateWithException { public static final Logger LOGGER = Logger.getLogger(ImagesController.class); @Autowired FromBlobMapper fromBlobMapper; /** * *@Title getIcons *@Description: 把本地的图片以流的形式输出到页面中去 *@Author fendo *@Date 2017年8月17日 上午10:52 *@param *@return void *@throws *@测试地址: localhost:8996/GZ/images/geticons */ @RequestMapping(value="geticons",method = RequestMethod.GET) public void getIcons(HttpServletRequest request, HttpServletResponse response) { try { FileInputStream is = new FileInputStream("G:\\images\\Icon\\valid.png"); int i = is.available(); // 得到文件大小 byte data[] = new byte[i]; is.read(data); // 读数据 is.close(); response.setContentType("image/*"); // 设置返回的文件类型 OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象 toClient.write(data); // 输出数据 toClient.close(); } catch (Exception e) { e.printStackTrace(); } } /** * *@Title getImages *@Description: 根据用户表的的imgguid去FORMBLOB中获取图片 *@Author fendo *@Date 2017年8月17日 上午10:52 *@param guid *@return void *@throws *@测试地址: localhost:8996/GZ/images/getimages?guid=269A037ADE14FE02E050AE0AC684ADFB */ @RequestMapping(value="getimages",method = RequestMethod.GET) public void getImages(String guid,HttpServletRequest request, HttpServletResponse response){ ServletOutputStream out = null; if(StringUtils.isNotBlank(guid)){ try { //根据id去图片表获取数据 FromBlob fromBlob = fromBlobMapper.selectByPrimaryKey(guid); //获取blob字段 byte[] contents = fromBlob.getBdata(); System.out.println("内容是:"+contents.length); InputStream is = new ByteArrayInputStream(contents); response.setContentType("image/*"); out = response.getOutputStream(); int len=0; byte[]buf=new byte[1024]; while((len=is.read(buf,0,1024))!=-1){ out.write(buf, 0, len); } out.flush(); out.close(); } catch (IOException e) { LOGGER.error(e); e.printStackTrace(); } } }}
实体类:
private String guid; private String grdid; private String sytid; private Date crtdat; private String fileurl; private String filename; private String formguid; private byte[] bdata;
BLOB类型的用byte[]来存储!!
前台页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>测试</title></head><body><!-- 获取id为1的blob类型图片,定义宽128,高185 --><img src="<%=request.getContextPath()%>/images/getimages?guid=269A037ADE14FE02E050AE0AC684ADFB" width="128" height="185"/><hr><img src="<%=request.getContextPath()%>/images/geticons" /></body></html>
效果如下:
通过AJAX调用如下:
<%-- Created by IntelliJ IDEA. User: fendo Date: 2017/8/22 Time: 13:16 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>测试</title> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script> $(function () { var xhr = new XMLHttpRequest(); xhr.open("get", "<%=request.getContextPath()%>/images/getimages?guid=5041BB6BB47520F0E053AA0012ACC24D", true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status == 200) { var blob = this.response; var img = document.createElement("img"); img.onload = function(e) { window.URL.revokeObjectURL(img.src); }; img.src = window.URL.createObjectURL(blob); img.width=100; img.height=100; $("#insertImages").html(img); } } xhr.send(); }) </script></head><body><!-- 获取id为1的blob类型图片,定义宽128,高185 --><img src="<%=request.getContextPath()%>/images/getimages?guid=5041BB6BB47520F0E053AA0012ACC24D" width="128" height="185"/><div id="insertImages"></div><hr><img src="<%=request.getContextPath()%>/images/geticons" /></body></html>
阅读全文
1 0
- Spring MVC返回BLOB类型的图片
- spring mvc返回类型
- spring mvc返回类型
- Spring MVC Controller 支持的返回类型
- spring mvc返回json 类型的 数据
- spring mvc controller返回值的类型
- spring mvc返回值类型
- spring mvc返回值类型
- Spring MVC学习:处理方法返回值的可选类型
- Spring MVC学习:处理方法返回值的可选类型
- Spring MVC处理方法返回值的可选类型
- spring mvc 控制器(Controller)中可以返回的类型
- Spring MVC:图片验证码的生成与返回
- 使用mysql的blob类型存取图片
- Spring Boot系列九 spring mvc的@RequestMapping支持的方法参数类型和返回类型
- spring mvc 返回值类型设定
- Spring mvc转向以及设置返回类型
- spring mvc @ResponseBody 返回枚举类型
- 慕课网(imooc)Selenium自动化学习笔记03:遍历打印课程列表及获取下一页报错处理
- 架构师入门笔记九 初识IO NIO AIO
- Android的Button按钮,ACTION_UP事件不触发解决方案
- 生产者/消费者问题的多种Java实现方式
- Collection,List,Set和Map用法和区别
- Spring MVC返回BLOB类型的图片
- jquery使用 Form submission canceled because the form is not connected
- 云上架构和传统IT架构的区别在哪里?(企业CIO、CTO必读)
- 注解anoation原理及自定义注解demo实现
- callback的理解与Java实例
- Object-C知识点 (四)NSObject的继承关系
- Gemini代码摘抄(一)Graph和init
- codeVS 1011 数的计算
- oracle--基本转换函数