英文:
Unable to upload file from on server to another
问题
我正在尝试将一个文件从我的服务器上传到另一个服务器。在执行此操作时,我遇到了以下错误:`Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]`
我已经在请求体中放置了'file'键。我无法弄清楚为什么会发生这种情况。
上传服务器:
```java
String url = "http://localhost:8441/api/jar/solve";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", Paths.get("pathToFile").toFile());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
接收上传的服务器:
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO> solveUsingJar(@RequestParam("file") MultipartFile file) {
}
<details>
<summary>英文:</summary>
I am trying to upload a file on my server to another server. While doing this
I am getting this error : `Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]
`
I have put the 'file' key in my body. I am unable to figure out why this is hapenning.
Uploading Server:
``` String url = "http://localhost:8441/api/jar/solve";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body
= new LinkedMultiValueMap<>();
body.add("file", Paths.get("pathToFile").toFile());
HttpEntity<MultiValueMap<String, Object>> requestEntity
= new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate
.postForEntity(url, requestEntity, String.class);
Server that we are uploading to:
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO> solveUsingJar(@RequestParam("file") MultipartFile file) {
}
答案1
得分: 0
STEP-1 : 在服务器 Server-1 上上传文件:
@PostMapping("/upload/profile")
public UploadFileResponse uploadProfileImg(@RequestPart("file") MultipartFile file)
{
return fileService.uploadProfileImg(file);
}
STEP-2 : 使用 RestTemplate 在服务器 Server-2 上上传该文件。
public String upload(MultipartFile file) throws IOException
{
ResponseEntity<UploadFileResponse> response = null;
try
{
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String uploadURI = KeyConstant.FILE_UPLOAD_API_URI+"/file"; // Server-2(文件目录)FILE_UPLOAD_API_URI :http://27.34.2.33:28181/upload/file
map.add("file", new FileSystemResource(convFile));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
response = restTemplate.exchange(uploadURI, HttpMethod.POST, requestEntity, UploadFileResponse.class);
}
catch(Exception e) {e.printStackTrace();}
return response.getBody();
}
STEP-3 : 服务器 Server-2 文件上传 Rest 控制器:
@PostMapping("/upload/file")
public UploadFileResponse uploadFileByDirectory(@RequestPart(value = "file") MultipartFile file)
{
return this.amazonClient.uploadFilebyDirectory(KeyConstant.AMAZON_S3_PATH, file);
}
英文:
I share some code to upload a file one server to another server.
STEP-1 : Upload a file in Server-1 :
@PostMapping("/upload/profile")
public UploadFileResponse uploadProfileImg(@RequestPart("file") MultipartFile file)
{
return fileService.uploadProfileImg(file);
}
STEP-2 : Upload that file in Server-2 using RestTemplate.
public String upload(MultipartFile file) throws IOException
{
ResponseEntity<UploadFileResponse> response = null;
try
{
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String uploadURI = KeyConstant.FILE_UPLOAD_API_URI+"/file"; // Server-2(file Directory) FILE_UPLOAD_API_URI : http://27.34.2.33:28181/upload/file
map.add("file", new FileSystemResource(convFile));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
response = restTemplate.exchange(uploadURI, HttpMethod.POST, requestEntity, UploadFileResponse.class);
}
catch(Exception e) {e.printStackTrace();}
return response.getBody();
}
STEP-3 : Server-2 File Upload Rest Controller :
@PostMapping("/upload/file")
public UploadFileResponse uploadFileByDirectory(@RequestPart(value = "file") MultipartFile file)
{
return this.amazonClient.uploadFilebyDirectory(KeyConstant.AMAZON_S3_PATH, file);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论