Wednesday, January 4, 2023

Upload files to SAP Business Objects in Extended ECM using REST API

 public static String edgeUpload(String parenId,String workspace_type,String type,String file_path,String name) {

byte[] authEncBytes = Base64.encodeBase64(Constants.authString.getBytes());

       String authStringEnc = new String(authEncBytes);

       

     MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();

     bodyMap.add(Constants.FILE, new FileSystemResource(new File(file_path)));

     bodyMap.add(Constants.pID, parenId);

     bodyMap.add(Constants.wType, workspace_type);

     bodyMap.add(Constants.fName, name);

     bodyMap.add(Constants.TYPE, type);

     HttpHeaders headers = new HttpHeaders();

     headers.setContentType(MediaType.MULTIPART_FORM_DATA);

     headers.add("Authorization", "Basic " + authStringEnc);

     HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);


     RestTemplate restTemplate = new RestTemplate();

     ResponseEntity<String> response = restTemplate.exchange(Constants.endPointUrl,

             HttpMethod.POST, requestEntity, String.class);

     //System.out.println("response status: " + response.getStatusCode());

     //System.out.println("response body: " + response.getBody());

     return response.getStatusCode().toString();

}