RestTemplate上传图片到指定接口

1。将图片上传到服务器,我是用spring的restTemplate工具类

@RequestMapping("/upload")
 public Map<String, Object> uploadFile(@RequestParam("myFile") MultipartFile myFile)
   throws IOException {
  Map<String, Object> map = new HashMap<>();
  map.put(“flag”, false);
  if (myFile.isEmpty()) {
   map.put(“msg”, "上传文件 不能为空!");
  }
  logger.info("================接收上传文件,文件名: " + myFile.getOriginalFilename());
  String fileName = myFile.getOriginalFilename();
  logger.info("文件保存至:{}", savePath+fileName);
  File fileDir = new File(savePath);
  if(!fileDir.exists()){
   fileDir.mkdirs();
  }
  String suffix = fileName.substring(fileName.lastIndexOf("."));
  fileName = fileName.substring(0, fileName.lastIndexOf("."))+new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date())+suffix;
  File file = new File(savePath+fileName);
  myFile.transferTo(file);
  try {
   FileSystemResource resource = new FileSystemResource(file);
   MultiValueMap<String, Object> mapFile = new LinkedMultiValueMap<>();
   mapFile.add("mfile", resource);
   HttpHeaders headers = new HttpHeaders();
   headers.add("Content-Type","multipart/form-data");
   HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(mapFile,headers);
   ResponseEntity<String> postForEntity = restTemplate.postForEntity(uploadPath, httpEntity, String.class);
   String body = postForEntity.getBody();
   logger.info("上传图片返回报文:{}",body);
   JSONObject respJSON = JSONObject.parseObject(body);
   Integer flag = respJSON.getInteger("flag");
   if(flag==1){
   map.put("flag", true);
   map.put("url", respJSON.getString("filePath"));
   }else{
    map.put("flag", false);
    map.put("msg", respJSON.getString("msg"));
   }
  } catch (Exception e) {
   logger.error("上传失败", e);
   map.put("flag", false);
   map.put("msg", "上传失败");
  }
  file.delete();
  return map;
 }

2.首先springmvc接收前端上传文件(单个文件)

 public Map<String, Object> uploadFile(@RequestParam("myFile") MultipartFile myFile)

3.将文件临时存在服务器上,上传成功再删除,我项目中存在tomcat下

  File file = new File(savePath+fileName);
  myFile.transferTo(file);

4.之所以要转成File文件是因为MultipartFile不能被FileSystemResource 接收

  FileSystemResource resource = new FileSystemResource(file);
   MultiValueMap<String, Object> mapFile = new LinkedMultiValueMap<>();
   mapFile.add("mfile", resource);

5.拼装请求头和请求体(uploadPath可以是任意目的地)

  HttpHeaders headers = new HttpHeaders();
   headers.add("Content-Type","multipart/form-data");
   HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(mapFile,headers);
   ResponseEntity<String> postForEntity = restTemplate.postForEntity(uploadPath, httpEntity, String.class);
   String body = postForEntity.getBody();
   logger.info("上传图片返回报文:{}",body);

6.剩下的就是处理返回报文了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值