0

I am new to resttemplate and trying to understand how to invoke below API using restTemplate

public ResponseEntity<String> upload(@ResquestPart("file") MultipartFIle file,
@RequestParam("path") String path){

//businness logic
}

I am trying to call above as below however it fails on "java.io.filenotfoundexception multipartfile resource [ABC.txt]cannot be resolved to absolute file path"

public void uploadFile() {

Path path = Paths.get("C:/ABC.txt"); 
byte[] content = null;

try{
    content =  Files.readAllBytes(path);   // All file is read in content variable 
} catch(final IOException e){
}

MultipartFile file = new MockMultipartFile("ABC.txt",content);
    

HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentType("Accept", Mediatype.APPLICATION_JSON_VALUE);
headers.setContentType("Content-type", Mediatype.APPLICATION_JSON_VALUE);

MultiValueMap<String, Object> obj = new LinkedMultiValueMap<String, Object>();
obj.add("file", file);
obj.add9("path", "/opt/apps");

HttpEntity<?> requestEntity = new HttpEntity<>(obj, headers);

String result = getRestTemplate().postForEntity("url", requestEntity, String.class);

}
Nichole
  • 189
  • 2
  • 7

0 Answers0