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

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.client.RestTemplate;

import java.io.File;

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

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


}