網站首頁 編程語言 正文
需求背景:
公司有一個上傳文件的內部接口,但是由于網絡路線沒有打通,導致外部不能直接訪問這個接口,現在需要新增一個外部能訪問的接口,然后通過這個外部接口代理到內部的文件上傳接口。
功能實現
關于RestTemplate發送數據的案例有很多,但是這次需要使用RestTemplate轉發MultipartFile文件,網上很多都說需要將要轉發的文件保存到本地作為零時文件,然后通過**new FileSystemResource(localFile)**后來發送文件。但是我覺得應該有更好的方法,通過查看MultipartFile的源碼,其中getResource()方法有這樣一段話,才解決了我的疑惑。
/**
* Return a Resource representation of this MultipartFile. This can be used
* as input to the {@code RestTemplate} or the {@code WebClient} to expose
* content length and the filename along with the InputStream.
* @return this MultipartFile adapted to the Resource contract
* @since 5.1
*/
default Resource getResource() {
return new MultipartFileResource(this);
}
MultipartFile官方提供了getResource()方法返回一個MultipartFile的資源形式,并且可以將返回都Resouce作為RestTemplate或者WebClient的輸入。那么RestTemplate轉發MultipartFile的實現就可以這樣實現:
@RequestMapping(method = RequestMethod.POST, value = "/upload/logFiles", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String receiveFile(@RequestParam String vin,
@RequestParam(required = false, value = "Command_session_ID") Object cmdSessionIdStr,
@RequestParam String progress, @RequestParam String sig, @RequestParam String iv,
@RequestHeader("Authorization") String token,
HttpServletRequest request) {
//獲取上傳文件列表
MultiValueMap<String, MultipartFile> multiFileMap = ((MultipartHttpServletRequest) request).getMultiFileMap();
//獲取文件列表第的第一個文件key
String first = (String) multiFileMap.keySet().iterator().next();
//獲取文件列表中第一個文件
MultipartFile file = (MultipartFile) multiFileMap.getFirst(first);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
//設置請求類型
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("vin", vin);
params.add("Command_session_ID", cmdSessionIdStr);
params.add("progress", progress);
params.add("sig", sig);
params.add("iv", iv);
//將獲取的MultipartFile文件放入HttpEntity中
params.add("file", file.getResource());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(logUpLoadUrl, HttpMethod.POST, requestEntity, String.class);
return responseEntity.getBody();
}
原文鏈接:https://blog.csdn.net/qq_43600166/article/details/124605110
- 上一篇:Liunx下使用SSH登錄遠程服務器
- 下一篇:Maven快照更新策略
相關推薦
- 2022-03-28 Go實現用戶每日限額的方法(例一天只能領三次福利)_Golang
- 2022-05-12 Nginx反向代理 對響應網頁中的字符串進行替換設置
- 2022-01-31 pytorch:tensor與numpy轉換 & .cpu.numpy()和.numpy()
- 2022-05-13 在 Dart 中更好地使用類和 Mixin
- 2022-10-09 Python?局部變量global詳解_python
- 2022-06-23 android中的adb命令學習_Android
- 2022-05-21 基于Python實現Hash算法_python
- 2022-07-14 python?udp如何實現同時收發信息_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支