后端把图片返回给前端

方式1:返回base64格式

import org.apache.commons.codec.binary.Base64

@RequestMapping("/get-face-picture")
    public String  getUserFacePicture(HttpServletRequest request, @RequestBody JSONObject obj, HttpServletResponse response) {

        String sessionId = request.getHeader("token");
      
	   //获得一个byte[]
        byte[] faceImageByte = XXXXXXXXXX;

        //转成base64格式
        String base64EncoderImg = Base64.encodeBase64String(faceImageByte);

        return "data:image/"+ "图片格式(如jpg,png)" +";base64,"+base64EncoderImg;
       
    }

方式2:返回二进制流

@RequestMapping("/get-face-picture")
    public void getUserFacePicture(HttpServletRequest request, @RequestBody JSONObject obj, HttpServletResponse response) {
        String sessionId = request.getHeader("token");
       
	   //获得一个byte[]
        byte[] faceImageByte = XXXXXXXXXX;

        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(faceImageByte);
            outputStream.flush();
        } catch (Exception e) {
            logger.error("回传前端人脸识别图片写入时异常error:{}", e);

        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
                logger.error("回传前端人脸识别图片关闭资源异常error:{}", e);
            }
        }

    }

你可能感兴趣的:(java,后端,前端,java)