web图片上传和数据库取出Blob传到页面显示图片

@RequestMapping(value="insertReagent",method=RequestMethod.POST)
    @ResponseBody
    public Integer insertReagent(@RequestParam("file")MultipartFile file,Reagent reagent){
        try {
            byte[] logo=file.getBytes();
            reagent.setImage_path(logo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = reagentservice.insertReagent(reagent);
        return i;
    }

method=RequestMethod.POST仅支持post方式
@RequestParam(“file”)MultipartFile 限定名字叫file的参数传入封装成MultipartFile 对象
byte[] logo=file.getBytes();取出文件的字符数组

从数据库取出字符数组,输出到页面

    @RequestMapping("reagentImage")
    public void reagentImage(String rea_id,HttpServletResponse resq) throws IOException{
        Reagent reagent = reagentservice.queryReagentId(rea_id);
        resq.setContentType("image/jpg");
        resq.getOutputStream().write(reagent.getImage_path());

    }

setContentType这 个方法设置发送到客户端的响应的内容类型
resq.getOutputStream().write(reagent.getImage_path());
返回一个记录二进制的响应数据的输出流

    <img id="image" alt="" src="/MedLab/reagentImage.do?rea_id=78">

页面img src直接指向方法

你可能感兴趣的:(数据库-记录)