springboot图片用二进制byte[]存放数据库,进行读取

 @RequestMapping(value="/{id}",method=RequestMethod.GET)
    public void getPhotoById (@PathVariable("id")Integer id, final HttpServletResponse response) throws Exception{  
        CustomerPicture find = customerPictureService.find(id);
        byte[] data = find.getPicture();  
        response.setContentType("image/jpeg");  
        response.setCharacterEncoding("UTF-8");  
        OutputStream outputSream = response.getOutputStream();
        outputSream.write(data);
        outputSream.flush();
        /*InputStream in = new ByteArrayInputStream(data);  
        int len = 0;  
        byte[] buf = new byte[1024];  
        while ((len = in.read(buf, 0, 1024)) != -1) {  
            outputSream.write(buf, 0, len);  
        }  
        outputSream.close(); *|
    }  
 @PostMapping("/insertCheckingRecord")
    public Result<CustomerPicture> insert(@RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
        Result<CustomerPicture> res = new Result<>();
        try {
        	CustomerPicture customerPicture = new CustomerPicture();
        	InputStream inputStream = file.getInputStream();
        	byte[] pictureData = new byte[(int) file.getSize()];
        	inputStream.read(pictureData);
        	customerPicture.setPicture(pictureData);
            CustomerPicture customerPicture1 = customerPictureService.insert(customerPicture);
            res.setStatus(StatusCode.SUCCESSFUL);
            res.setData(customerPicture1);
            res.setMessage(StatusCode.ADD_SUCCESSFUL_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            res.setStatus(StatusCode.FAILED);
            res.setMessage(StatusCode.ADD_FAILED_MSG + e.getMessage());
        }
        return res;
    }

转载:https://blog.csdn.net/jiawenlong_/article/details/41553375

猜你喜欢

转载自blog.csdn.net/sinat_21184471/article/details/81877232