Uploading to QNAP NAS from my backend and image in the NAS ends up as a broken image

88 Views Asked by At

I'm using the File Station API. I have this code in my backend (Spring) to upload files to NAS:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>(); 

ContentDisposition contentDisposition = ContentDisposition 
.builder("form-data") 
.name("file") 
.filename(multipartFile.getOriginalFilename()) 
.build(); 
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()); 
HttpEntity<byte[]> fileEntity = new HttpEntity<>(multipartFile.getBytes(), fileMap);

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); body.add("file", fileEntity);

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

String response = restTemplate.exchange(endpoint, HttpMethod.POST, requestEntity, String.class).getBody();

return response;

When I open the file in NAS, it's already a broken image.

enter image description here

I tested the code on the backend itself (calls a rest API within itself to upload a file) and it uploads successfully without breaking the image.

0

There are 0 best solutions below