HttpUtils.java
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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");
}
}