項目過程中,經(jīng)常會有和第三方接口打交道的過程,今天實現(xiàn)調(diào)用第三方上傳文件的接口??!
通常拿到第三方的接口文檔的時候,不是第一時間先寫代碼,而是詳細閱讀接口文檔。若接口需要第三方提供的基本參數(shù),例如signkey, secrect等,也可以是其他的,查看文檔里是否提供。再用工具(postman、swagger)構(gòu)建接口請求參數(shù)進行接口測試,測試成功后, 再開始寫代碼??!
問題說明
1、我們自己的封裝接口上傳文件類型為MultipartFile
2、上傳文件不是到我們服務(wù)器,而是到第三方接口上
3、使用hutool HttpRequest工具進行調(diào)用
4、直接傳入MultipartFile是不可用的
1、SpringBoot實現(xiàn)文件上傳-支持多文件
文件上傳,參數(shù)用MultipartFile類型接收
編寫contoller層接口
接口是post請求方式,參數(shù)用MultipartFile類型接收
@ApiOperation(value = "多文件上傳")
@PostMapping("/upload/file")
public RtnResult<Object> uploadAttatchmentFile(MultipartFile[] file) {
return mailService.uploadAttatchmentFile(file);
}
編寫service層方法
邏輯:
a.先構(gòu)建請求URL
b. 按第三方接口文檔處理多文件上傳請求參數(shù),構(gòu)建HttpHeaders,HttpEntity
c.使用RestTemplate請求第三方接口
d.處理返回結(jié)果
public RtnResult<Object> uploadAttatchmentFile(MultipartFile[] files) {
String url = urlPrefix + UPLOAD_ATTACHMENT_FILE_URL;
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
for (MultipartFile file : files) {
param.add("file", file.getResource());
}
RestTemplate request = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(param, headers);
ResponseEntity<Map> response = request.postForEntity(url, requestEntity, Map.class);
if (response.getStatusCode() != HttpStatus.OK) {
return (RtnResult.errorCodeMsg.SERVER_ERROR);
}
Map result = response.getBody();
return RtnResult.success(result);
}
2、調(diào)用第三方接口post請求多文件上傳文件
上傳調(diào)用代碼
注意:transferToFile()這個方法時最主要的,我們需要轉(zhuǎn)換File類型進行第三方上傳
public R uploadByte(String attachType, MultipartFile file) {
File toFile = transferToFile(file);
Map data = new HashMap();
data.put("file", toFile);
data.put("attachType", attachType);
String body = HttpRequest.post("第三方url")
.form(data)
.contentType("multipart/form-data")
.execute()
.body();
Map result = new HashMap();
if (StrUtil.isNotBlank(body)) {
result = JSON.parseObject(body, Map.class);
}
return R.ok().put("data", result);
}
MultipartFile 轉(zhuǎn) File(第一種方法)
public File transferToFile(MultipartFile multipartFile) {
//選擇用緩沖區(qū)來實現(xiàn)這個轉(zhuǎn)換即使用java 創(chuàng)建的臨時文件 使用 MultipartFile.transferto()方法 。
File file = null;
try {
String originalFilename = multipartFile.getOriginalFilename();
//獲取文件后綴
String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
file = File.createTempFile(originalFilename, prefix); //創(chuàng)建零食文件
multipartFile.transferTo(file);
//刪除
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
MultipartFile 轉(zhuǎn) File(第二種方法)文章來源:http://www.zghlxwxcb.cn/news/detail-597524.html
/**
* MultipartFile 轉(zhuǎn) File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
//獲取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
通過上述操作、測試,成功調(diào)用第三方上傳文件接口?。?!文章來源地址http://www.zghlxwxcb.cn/news/detail-597524.html
到了這里,關(guān)于Springboot實現(xiàn)上傳文件,并實現(xiàn)調(diào)用第三方接口post請求多文件上傳文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!