今天想實現 java 后端發送 formdata 上傳文件,為了以后查找方便,特此記錄下來
上一次使用 WebClient 實現遠程調用 (一個非阻塞、響應式的HTTP客戶端,它以響應式被壓流的方式執行HTTP請求) 查看
現在使用的 RestTemplate
RestTemplate 是用於同步client端訪問 Restful 服務的一個核心類
默認使用 JDK 提供的包去建立HTTP連接
為每種 HTTP 請求都實現了相關的請求封裝方法,根據HTTP的六個方法制定
HTTP method | RestTemplate methods |
---|---|
DELETE | delete |
GET | getForObject |
getForEntity | |
HEAD | headForHeaders |
OPTIONS | optionsForAllow |
POST | postForLocation |
postForObject | |
PUT | put |
any | exchange |
execute |
eg:后端實現文件上傳
(1)public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
參數:
url -> URI類型的請求路徑
request -> 請求體對象
responseType -> 響應數據類型
返回值:
響應消息體的內容
package com.example.hystrix.controller; import org.springframework.core.io.FileSystemResource; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.io.File; @RestController public class DemoController { @RequestMapping("/upload") public String upload() { String url = "http://localhost:2001/api/upload"; //上傳的地址 String filePath = "E:\\test\\test.dxf"; RestTemplate rest = new RestTemplate(); FileSystemResource resource = new FileSystemResource(new File(filePath)); MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("files", resource); //MultipartFile的名稱 String rs = rest.postForObject(url, param, String.class); System.out.println(rs); return rs; } }
說明:
exchange()方法跟上面的getForObject()、getForEntity()、postForObject()、postForEntity()等方法不同之處在於它可以指定請求的HTTP類型
MultiValueMap 是 Map 的一個子類,它的一個 key 可以存儲多個 value
(2)public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)
參數:
url -> URI類型的請求路徑
method-> 請求方式
requestEntity-> 請求體
responseType -> 響應數據類型
返回值:
Spring對HTTP請求響應的封裝,包括了響應碼、contentType、contentLength、響應消息體等
package com.example.hystrix.controller; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.io.File; @RestController public class DemoController { @RequestMapping("/upload") public String upload() { String url = "http://localhost:2001/api/upload"; //上傳的地址 String filePath = "E:\\test\\test.dxf"; RestTemplate rest = new RestTemplate(); FileSystemResource resource = new FileSystemResource(new File(filePath)); MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("files", resource); //MultipartFile的名稱 HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param); ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class); String rs = responseEntity.getBody(); System.out.println(rs); return rs; } }
說明:
exchange()方法跟上面的getForObject()、getForEntity()、postForObject()、postForEntity()等方法不同之處在於它可以指定請求的HTTP類型
eg,其他:
發送Get請求
(1)public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
參數:
url -> URI類型的請求路徑
responseType -> 響應數據類型
uriVariables-> URL變量
返回值:
Spring對HTTP請求響應的封裝,包括了響應碼、contentType、contentLength、響應消息體等
package com.example.hystrix.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; @RestController public class DemoController { @RequestMapping("/test") public String test() { RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:2001/api/test?name={name}&text={text}"; Map<String, String> params = new HashMap<>(); params.put("name", "baby"); params.put("text", "aaa"); ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params); String response = responseEntity.getBody(); return response; } }
(2)public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
參數:
url -> URI類型的請求路徑
responseType -> 響應數據類型
uriVariables-> URL變量
返回值:
響應消息體的內容
package com.example.hystrix.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; @RestController public class DemoController { @RequestMapping("/test") public String test() { RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:2001/api/test?name={name}&text={text}"; Map<String, String> params = new HashMap<>(); params.put("name", "baby"); params.put("text", "aaa"); String response = restTemplate.getForObject(url, String.class, params); return response; } }
說明:
url里使用name={name}的形式,最后一個參數是一個map,map的key即為前邊占位符的名字,map的value為參數值
只關注返回的消息體的內容,對其他信息都不關注,可以使用getForObject