HttpUtils.java 3.79 KB
package com.example.dahua.utils;

import com.example.dahua.bean.PermissionBean;
import com.example.dahua.lib.CompressPic;
import com.example.dahua.lib.FilePath;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.File;

public class HttpUtils {

    /**
     * 单个人脸下发至设备
     * 文件先由文件服务上传至100服务上,在21服务进行下发动作
     * @param file
     * @param schoolId
     * @param studentCode
     * @param userType
     * @param deviceId
     * @return
     */
    public static boolean uploadImg(File file, Integer schoolId, String studentCode,Integer userType, String deviceId) {
        if (!file.exists()) return false;
        String targPath = FilePath.picPathComp + studentCode + ".jpg";
        String url = "http://116.62.155.137:8991/file/uploadImg";
        MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = null;
        ResponseEntity<String> responseEntity = null;
        try {
            CompressPic.CompressPic(file.getAbsolutePath(), targPath);//压缩后的图片
            MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
            headers.setContentType(mediaType);
            FileSystemResource fileSystemResource = new FileSystemResource(targPath);

            multivaluedMap.add("file", fileSystemResource);
            multivaluedMap.add("schoolId", schoolId);
            multivaluedMap.add("studentCode", studentCode);
            multivaluedMap.add("userType", userType);
            if(!StringUtils.isEmpty(deviceId)){
                multivaluedMap.add("deviceId", deviceId);
            }
            httpEntity = new HttpEntity<>(multivaluedMap, headers);
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
            System.out.println("responseEntity:" + responseEntity.getBody());
            return responseEntity.getBody().equals("1");
        } catch (Exception e) {
            e.printStackTrace();
            return responseEntity.getBody().equals("0");
        }
    }


    public static void deleteFace(Integer schoolId) {
        String url = "http://116.62.155.137:8991/operate/deleteFailFace?schoolId=" + schoolId;
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Boolean> result = restTemplate.getForEntity(url, Boolean.class);
        System.out.println("发生请求21服务删除人脸,请求地址: +" + url+ "返回信息: "+ result.getBody());
    }

    /**
     * 权限下发至设备
     * @param permissionBean
     * @return
     */
    public static boolean sendPermission(PermissionBean permissionBean) {
        String url = "http://116.62.155.137:8991/file/sendPermission";
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(mediaType);
        HttpEntity<PermissionBean> httpEntity = new HttpEntity<>(permissionBean, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        System.out.println("大华设备下发权限:" + responseEntity.getBody());
        return responseEntity.getBody().equals("1");
    }
}