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

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

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 &#39;file&#39; is not present]
`

I have put the &#39;file&#39; key in my body. I am unable to figure out why this is hapenning.

Uploading Server:
``` String url = &quot;http://localhost:8441/api/jar/solve&quot;;
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);
                MultiValueMap&lt;String, Object&gt; body
                        = new LinkedMultiValueMap&lt;&gt;();
                body.add(&quot;file&quot;, Paths.get(&quot;pathToFile&quot;).toFile());
                HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; requestEntity
                        = new HttpEntity&lt;&gt;(body, headers);

                RestTemplate restTemplate = new RestTemplate();
                ResponseEntity&lt;String&gt; response = restTemplate
                        .postForEntity(url, requestEntity, String.class);

Server that we are uploading to:

@RequestMapping(value = &quot;/api&quot;, method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity&lt;ResponseDTO&gt; solveUsingJar(@RequestParam(&quot;file&quot;) 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(&quot;/upload/profile&quot;)
public UploadFileResponse uploadProfileImg(@RequestPart(&quot;file&quot;) MultipartFile file)
{		
	return fileService.uploadProfileImg(file);
}

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

public String upload(MultipartFile file) throws IOException 
{
	ResponseEntity&lt;UploadFileResponse&gt; response = null;
	 
	try
	{
		File convFile = new File(file.getOriginalFilename());		    
		
		convFile.createNewFile();		
		
		FileOutputStream fos = new FileOutputStream(convFile);		    
		fos.write(file.getBytes());
		fos.close();
		
		LinkedMultiValueMap&lt;String, Object&gt; map = new LinkedMultiValueMap&lt;&gt;();
		 
		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
			
		map.add(&quot;file&quot;,  new FileSystemResource(convFile));
		
		HttpHeaders headers = new HttpHeaders();
		
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		
		HttpEntity&lt;LinkedMultiValueMap&lt;String, Object&gt;&gt; requestEntity = new HttpEntity&lt;&gt;(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(&quot;/upload/file&quot;)
public UploadFileResponse uploadFileByDirectory(@RequestPart(value = &quot;file&quot;) MultipartFile file)
{
	return this.amazonClient.uploadFilebyDirectory(KeyConstant.AMAZON_S3_PATH, file);
}

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:

确定