当前位置:   article > 正文

RestTemplate发送MultipartFile为参数的请求

resttemplate发送multipartfile
  1. package com.rh.user.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.rh.user.untils.JsonFileUtils;
  5. import com.rh.user.untils.RestTemplateUtils;
  6. import com.rh.user.untils.ServiceNode;
  7. import feign.Param;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Qualifier;
  10. import org.springframework.core.io.ByteArrayResource;
  11. import org.springframework.core.io.ClassPathResource;
  12. import org.springframework.core.io.Resource;
  13. import org.springframework.http.*;
  14. import org.springframework.util.LinkedMultiValueMap;
  15. import org.springframework.util.MultiValueMap;
  16. import org.springframework.web.bind.annotation.*;
  17. import org.springframework.web.client.RestTemplate;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. @RestController
  27. public class ControllerUtils {
  28. @Autowired
  29. @Qualifier("restTemplate")
  30. private RestTemplate restTemplate;
  31. @Autowired
  32. @Qualifier("loadBalanced")
  33. private RestTemplate loadBalanced;
  34. private static Map<String, ServiceNode> serviceMap;
  35. private static JSONArray array;
  36. static {
  37. Resource resource = new ClassPathResource("init.json");
  38. InputStream inputStream = null;
  39. //String path = ControllerUtils.class.getClassLoader().getResource("init.json").getPath();
  40. try {
  41. inputStream = resource.getInputStream();
  42. array = JsonFileUtils.getJsonArray(inputStream);
  43. serviceMap = new HashMap<>();
  44. System.out.println(array.toJSONString());
  45. for (int i = 0; i < array.size(); i++) {
  46. ServiceNode serviceNode = JSON.toJavaObject((JSON) array.get(i), ServiceNode.class);
  47. serviceMap.put(serviceNode.getService(), serviceNode);
  48. }
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. if (inputStream != null) {
  52. try {
  53. inputStream.close();
  54. } catch (IOException e1) {
  55. e1.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. @RequestMapping(value = "/transform2/{path}", method = RequestMethod.POST)
  61. public Object transfrom2(@PathVariable("path") String path, @RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
  62. ServiceNode serviceNode = serviceMap.get(path);
  63. ByteArrayResource fileAsResource = new ByteArrayResource(multipartFile.getBytes()) {
  64. @Override
  65. public String getFilename() {
  66. return multipartFile.getOriginalFilename();
  67. }
  68. @Override
  69. public long contentLength() {
  70. return multipartFile.getSize();
  71. }
  72. };
  73. MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
  74. multipartRequest.add("multipartFile", fileAsResource);
  75. HttpHeaders headers = new HttpHeaders();
  76. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  77. HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(multipartRequest, headers);
  78. //发起调用
  79. return loadBalanced.postForObject(serviceNode.getUrl(), requestEntity, Object.class);
  80. }
  81. @RequestMapping("/transform/{path}")
  82. public Object transfrom(@PathVariable("path") String path, @RequestBody String parms) {
  83. ServiceNode serviceNode = serviceMap.get(path);
  84. HttpEntity<String> httpEntity = RestTemplateUtils.createHttpEntity(parms);
  85. if (serviceNode.getTemplate().equals("restTemplate")) {
  86. return restTemplate.postForObject(serviceNode.getUrl(), httpEntity, Object.class);
  87. } else if (serviceNode.getTemplate().equals("loadBalanced")) {
  88. return loadBalanced.postForObject(serviceNode.getUrl(), httpEntity, Object.class);
  89. } else {
  90. return "不支持的格式";
  91. }
  92. }
  93. @RequestMapping(value = "/transform1/{path}", method = RequestMethod.GET)
  94. public void transform1(@PathVariable("path") String path, @Param("id") int id, HttpServletRequest request, HttpServletResponse response) throws IOException {
  95. HttpHeaders headers = new HttpHeaders();
  96. Map<String, String[]> parameterMap = request.getParameterMap();
  97. String par = "";
  98. for (String key : parameterMap.keySet()) {
  99. par += key + "=" + parameterMap.get(key)[0] + "&";
  100. }
  101. if (par.endsWith("&")) {
  102. par = par.substring(0, par.length() - 1);
  103. }
  104. HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
  105. ServiceNode serviceNode = serviceMap.get(path);
  106. String url = serviceNode.getUrl();
  107. if (par.length() != 0) {
  108. url += "?" + par;
  109. }
  110. ResponseEntity<byte[]> response1 = loadBalanced.exchange(url,
  111. HttpMethod.GET,
  112. httpEntity, byte[].class);
  113. HttpHeaders headers1 = response1.getHeaders();
  114. response.setHeader("Content-Disposition", headers1.getContentDisposition().toString());
  115. OutputStream outputStream = response.getOutputStream();
  116. outputStream.write(response1.getBody());
  117. outputStream.flush();
  118. outputStream.close();
  119. }
  120. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/302876
推荐阅读
相关标签
  

闽ICP备14008679号