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

import com.example.dahua.bean.PermissionBean;
import com.example.dahua.bean.PermissionFaceBean;
import com.example.dahua.bean.PermissionHKBean;
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.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class HttpUtils {

    /**
     * 单个人脸下发至设备
     * 文件先由文件服务上传至100服务上,在21服务进行下发动作
     * @param file
     * @param schoolId
     * @param studentCode
     * @param clint_type
     * @param userType
     * @param deviceId
     * @return
     */
    public static boolean uploadImgs(File file, String schoolId, String studentCode, String clint_type, int userType,String deviceId) {

        if (!file.exists()) {
            System.out.println("图片不存在");
            return false;
        }
        String targPath = FilePath.picPathComp + studentCode + ".jpg";
        String url = "http://121.40.109.21: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, studentCode);//压缩后的图片

            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("clint_type", clint_type);
            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");
        }
    }

    /**
     * 下发用户信息及人脸
     * @param file
     * @param schoolId
     * @param studentCode
     * @param clintType
     * @param deviceId
     * @param userType
     * @return
     */
    public static boolean sendUserAndImg(File file, String schoolId, String studentCode, String clintType, String deviceId,int userType) {

        String targetPath = FilePath.picPathComp + studentCode + FilePath.fileSuffix;

        String url = "http://121.40.109.21:8991/operate/sendUserAndImg";
        //String url = "http://localhost: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(), targetPath, studentCode);

            MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);

            headers.setContentType(mediaType);

            FileSystemResource fileSystemResource = new FileSystemResource(targetPath);

            multivaluedMap.add("file", fileSystemResource);
            multivaluedMap.add("schoolId", schoolId);
            multivaluedMap.add("studentCode", studentCode);
            multivaluedMap.add("clintType", clintType);
            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 String uploadImg2HK(String filePath,String card,String name,String deviceId,String userType){

        String url = String.format("http://114.55.30.100:8089/facereco/sendCardAndImg1?filePath=%s&card=%s&name=%s&userType=%s&deviceId=%s",
                filePath,card,name,userType,deviceId);

        RestTemplate restTemplate = new RestTemplate();

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);

        System.out.println("responseEntity:" + responseEntity.getBody());
        return responseEntity.getBody();
    }


    public static boolean imgsSend(String schoolId, int type) {
        String url = "http://121.40.109.21:8991/file/imgsSend?schoolId=" + schoolId + "&type=" + type;
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Boolean> result = restTemplate.getForEntity(url, Boolean.class);
        System.out.println("result:" + result.getBody());
        return result.getBody();
    }

    public static void deleteFace(Integer schoolId) {
        String url = "http://121.40.109.21: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://121.40.109.21:8991/file/sendPermission";
//        String url = "http://localhost: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");
    }

    /**
     * 权限下发至海康设备
     * @param permissionFaceBean
     */
    public static void sendPermission2HK(PermissionFaceBean permissionFaceBean) {
        String url = "http://114.55.30.100:8089/facereco/sendPermiss";
        PermissionBean permissionBean = permissionFaceBean.getPermissionBean();

        if (null!=permissionBean){

            PermissionHKBean permissionHKBean = new PermissionHKBean();

            permissionHKBean.setDeviceIds(permissionBean.getDeviceIds());
            permissionHKBean.setSchoolId(permissionFaceBean.getSchoolId()+"");
            permissionHKBean.setUserType("2");
            permissionHKBean.setWeekDays(permissionBean.getWeekDays());

            RestTemplate restTemplate = new RestTemplate();

            HttpHeaders headers = new HttpHeaders();

            MediaType mediaType = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);

            headers.setContentType(mediaType);

            HttpEntity<PermissionHKBean> httpEntity = new HttpEntity<>(permissionHKBean, headers);

            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);

            System.out.println("海康设备下发权限:" + responseEntity.getBody());

        }

    }
}