79287151

Date: 2024-12-17 08:39:23
Score: 2.5
Natty:
Report link

@Carol Sharing code snippet

// Read the file into a byte array
byte[] fileBytes = Files.readAllBytes(filePath);

// Set headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

// Create the HTTP entity
HttpEntity<byte[]> entity = new HttpEntity<>(fileBytes, headers);

// Execute the PUT request
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(
    signedUrl,
    HttpMethod.PUT,
    entity,
    String.class
);

if (response.getStatusCode() == HttpStatus.OK) {
   return "Upload successful!";
} else {
   return "Upload failed: " + response.getStatusCode();
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Carol
  • Self-answer (0.5):
  • Looks like a comment (1):
  • Low reputation (1):
Posted by: Suvarna Raverkar