HttpUtils.java 5.38 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.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 {

    public static boolean uploadImgs(File file, String schoolId, String studentCode, String clint_type, int userType) {

        if (!file.exists()) {
            System.out.println("图片不存在");
            return false;
        }
        String targPath = FilePath.picPathComp + studentCode + ".jpg";
//            String targPath =  "C:\\Users\\taohandong\\Desktop\\comp\\"+userInfoBean.getStudentcode()+".jpg";
        try {
            CompressPic.CompressPic(file.getAbsolutePath(), targPath, studentCode);//压缩后的图片
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = "http://121.40.109.21:8991/file/uploadImg";
//        String url = "http://localhost:8991/file/uploadImg";
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();

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

        headers.setContentType(mediaType);

        MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();

        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);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multivaluedMap, headers);

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

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


    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 boolean sendPermission(PermissionBean permissionBean) {
        String url = "http://121.40.109.21: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");
    }

    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());

        }

    }
}