无法将文件从一个服务器上传到另一个。

huangapple go评论106阅读模式
英文:

Unable to upload file from on server to another

问题

  1. 我正在尝试将一个文件从我的服务器上传到另一个服务器。在执行此操作时,我遇到了以下错误:`Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]`
  2. 我已经在请求体中放置了'file'键。我无法弄清楚为什么会发生这种情况。
  3. 上传服务器:
  4. ```java
  5. String url = "http://localhost:8441/api/jar/solve";
  6. HttpHeaders headers = new HttpHeaders();
  7. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  8. MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
  9. body.add("file", Paths.get("pathToFile").toFile());
  10. HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
  11. RestTemplate restTemplate = new RestTemplate();
  12. ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

接收上传的服务器:

  1. @RequestMapping(value = "/api", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  2. public ResponseEntity<ResponseDTO> solveUsingJar(@RequestParam("file") MultipartFile file) {
  3. }
  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to upload a file on my server to another server. While doing this
  4. I am getting this error : `Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part &#39;file&#39; is not present]
  5. `
  6. I have put the &#39;file&#39; key in my body. I am unable to figure out why this is hapenning.
  7. Uploading Server:
  8. ``` String url = &quot;http://localhost:8441/api/jar/solve&quot;;
  9. HttpHeaders headers = new HttpHeaders();
  10. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  11. MultiValueMap&lt;String, Object&gt; body
  12. = new LinkedMultiValueMap&lt;&gt;();
  13. body.add(&quot;file&quot;, Paths.get(&quot;pathToFile&quot;).toFile());
  14. HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; requestEntity
  15. = new HttpEntity&lt;&gt;(body, headers);
  16. RestTemplate restTemplate = new RestTemplate();
  17. ResponseEntity&lt;String&gt; response = restTemplate
  18. .postForEntity(url, requestEntity, String.class);

Server that we are uploading to:

  1. @RequestMapping(value = &quot;/api&quot;, method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  2. public ResponseEntity&lt;ResponseDTO&gt; solveUsingJar(@RequestParam(&quot;file&quot;) MultipartFile file) {
  3. }

答案1

得分: 0

  1. STEP-1 : 在服务器 Server-1 上上传文件:
  2. @PostMapping("/upload/profile")
  3. public UploadFileResponse uploadProfileImg(@RequestPart("file") MultipartFile file)
  4. {
  5. return fileService.uploadProfileImg(file);
  6. }
  7. STEP-2 : 使用 RestTemplate 在服务器 Server-2 上上传该文件。
  8. public String upload(MultipartFile file) throws IOException
  9. {
  10. ResponseEntity<UploadFileResponse> response = null;
  11. try
  12. {
  13. File convFile = new File(file.getOriginalFilename());
  14. convFile.createNewFile();
  15. FileOutputStream fos = new FileOutputStream(convFile);
  16. fos.write(file.getBytes());
  17. fos.close();
  18. LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
  19. String uploadURI = KeyConstant.FILE_UPLOAD_API_URI+"/file"; // Server-2(文件目录)FILE_UPLOAD_API_URI :http://27.34.2.33:28181/upload/file
  20. map.add("file", new FileSystemResource(convFile));
  21. HttpHeaders headers = new HttpHeaders();
  22. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  23. HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
  24. RestTemplate restTemplate = new RestTemplate();
  25. response = restTemplate.exchange(uploadURI, HttpMethod.POST, requestEntity, UploadFileResponse.class);
  26. }
  27. catch(Exception e) {e.printStackTrace();}
  28. return response.getBody();
  29. }
  30. STEP-3 : 服务器 Server-2 文件上传 Rest 控制器:
  31. @PostMapping("/upload/file")
  32. public UploadFileResponse uploadFileByDirectory(@RequestPart(value = "file") MultipartFile file)
  33. {
  34. return this.amazonClient.uploadFilebyDirectory(KeyConstant.AMAZON_S3_PATH, file);
  35. }
英文:

I share some code to upload a file one server to another server.

STEP-1 : Upload a file in Server-1 :

  1. @PostMapping(&quot;/upload/profile&quot;)
  2. public UploadFileResponse uploadProfileImg(@RequestPart(&quot;file&quot;) MultipartFile file)
  3. {
  4. return fileService.uploadProfileImg(file);
  5. }

STEP-2 : Upload that file in Server-2 using RestTemplate.

  1. public String upload(MultipartFile file) throws IOException
  2. {
  3. ResponseEntity&lt;UploadFileResponse&gt; response = null;
  4. try
  5. {
  6. File convFile = new File(file.getOriginalFilename());
  7. convFile.createNewFile();
  8. FileOutputStream fos = new FileOutputStream(convFile);
  9. fos.write(file.getBytes());
  10. fos.close();
  11. LinkedMultiValueMap&lt;String, Object&gt; map = new LinkedMultiValueMap&lt;&gt;();
  12. String uploadURI = KeyConstant.FILE_UPLOAD_API_URI+&quot;/file&quot;; // Server-2(file Directory) FILE_UPLOAD_API_URI : http://27.34.2.33:28181/upload/file
  13. map.add(&quot;file&quot;, new FileSystemResource(convFile));
  14. HttpHeaders headers = new HttpHeaders();
  15. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  16. HttpEntity&lt;LinkedMultiValueMap&lt;String, Object&gt;&gt; requestEntity = new HttpEntity&lt;&gt;(map, headers);
  17. RestTemplate restTemplate = new RestTemplate();
  18. response = restTemplate.exchange(uploadURI, HttpMethod.POST, requestEntity, UploadFileResponse.class);
  19. }
  20. catch(Exception e) {e.printStackTrace();}
  21. return response.getBody();
  22. }

STEP-3 : Server-2 File Upload Rest Controller :

  1. @PostMapping(&quot;/upload/file&quot;)
  2. public UploadFileResponse uploadFileByDirectory(@RequestPart(value = &quot;file&quot;) MultipartFile file)
  3. {
  4. return this.amazonClient.uploadFilebyDirectory(KeyConstant.AMAZON_S3_PATH, file);
  5. }

huangapple
  • 本文由 发表于 2020年4月5日 19:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定