java实现调用第三方接口post请求上传文件请求参数为MultipartFile携带token的解决方案
控制器层:
/**
* 上传名片
*
* @param file 文件流
* @return 返回结果
*/
@PostMapping("/upload_image")
public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) {
return iApiUserCenterService.uploadImage(file);
}
```
**serviceimpl:**
/**
* 上传名片
*/
@Override
public AjaxResult uploadImage(MultipartFile file) {
AjaxResult ajaxResult = new AjaxResult();
try {
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", new FileSystemResource(convert(file)));
String url = this.url + "/image/upload";
String res = httpPostUtils.JPost(url,file,httpSession.getAttribute("token").toString());
if ("".equals(res) || res == null) {
logger.info("上传名片失败,调用官网接口返回结果为空->{}", res);
ajaxResult.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR);
ajaxResult.put(AjaxResult.MSG_TAG, "上传名片失败,调用官网接口返回结果为空");
return ajaxResult;
}
//将返回数据转换成json
JSONObject jsonObject = JSONObject.parseObject(res);
String message = (jsonObject.get("msg")).toString();
String code = (jsonObject.get("code")).toString();
JSONObject dataObject = (JSONObject) jsonObject.get("data");
String path = dataObject.getString("path");
//TODO 需要将返回的图片路径进行保存
if ("1".equals(code)) {
ajaxResult.put(AjaxResult.CODE_TAG, AjaxResult.Type.SUCCESS);
ajaxResult.put(AjaxResult.MSG_TAG, message);
ajaxResult.put(AjaxResult.DATA_TAG, path);
} else {
ajaxResult.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR);
ajaxResult.put(AjaxResult.MSG_TAG, message);
}
logger.info("上传名片接口调用结束结束,响应结果{}", res);
} catch (Exception e) {
ajaxResult.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR);
ajaxResult.put(AjaxResult.MSG_TAG, "调用上传名片接口异常");
logger.info("调用上传名片接口异常,错误信息{}", e.getMessage());
}
return ajaxResult;
}
## 重要的util
/**
*
* 发送文件MultipartFile类型的参数请求第三方接口
* @param url 请求url
* @param file 参数
* @return 字符流
* @throws IOException
*/
public String JPost(String url, MultipartFile file, String token) throws IOException {
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", new FileSystemResource(convert(file)));
HttpHeaders headers = new HttpHeaders();
headers.add("accept", "*/*");
headers.add("connection", "Keep-Alive");
headers.add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
headers.add("Accept-Charset", "utf-8");
headers.add("Content-Type", "application/json; charset=utf-8");
headers.add("token", token);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String body = response.getBody();
return body;
}
/**
* 接收处理传过来的文件
* @param file MultipartFile 类型的文件
* @return
*/
public static File convert(MultipartFile file) {
File convFile = new File(file.getOriginalFilename());
try {
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return convFile;
}
以上成功解决调用第三方接口上传文件问题,但是会在根目录下创建一个文件需要重新回调删除有待解决