YLombardi
YLombardi

Reputation: 1795

Multipart file upload with spring RestTemplate and Jackson

I want to upload a file by calling a rest web-service. This web-service need a MultipartFile.

I read here that I can do this : Multipart File Upload Using Spring Rest Template + Spring Web MVC

So, here is my code :

public Document uploadDocument(MultipartFile file) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(backendURL + "documents/upload");
        URI uri = builder.build().encode().toUri();

        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("file", file);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity =
                new HttpEntity<LinkedMultiValueMap<String, Object>>(map, headers);

        try {
            ResponseEntity<Document> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, Document.class);
        } catch (Exception e) {
            e.getMessage(); // Crash here
        }

        return document.getBody();
    }

Jackson try to serialize the file in JSON, but it fail with this error :

Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])

What can I do to disable the json serialization of the file ?

Upvotes: 1

Views: 10642

Answers (1)

YLombardi
YLombardi

Reputation: 1795

Thanks to Jekin Kalariya I found a working solution.

I create a temporary file from my MultipartFile and use it to create a FileSystemResource. I send this FileSystemResource instead of the MultipartFile.

Here is the working code :

public DocumentDetailed uploadDocumentInIfs(MultipartFile file, String userProfile) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(backendURL + "documents/upload");
    builder.queryParam("user", userProfile);
    URI uri = builder.build().encode().toUri();

    File tempFile = null;
    try {
        String extension = "." + getFileExtention(file.getOriginalFilename());
        tempFile = File.createTempFile("temp", extension);
        file.transferTo(tempFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("file", new FileSystemResource(tempFile));
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);

    Document document = null;
    try {
        ResponseEntity<Document> responseEntity =
                restTemplate.exchange(uri, HttpMethod.POST, requestEntity, Document.class);
        document = responseEntity.getBody();
    } catch (Exception e) {
        e.getMessage();
    }

    return document;
}

Upvotes: 3

Related Questions