Home > database >  Spring Content 1.2.5 JPA(Postgres) .docx file mutates to zip archive
Spring Content 1.2.5 JPA(Postgres) .docx file mutates to zip archive

Time:10-28

Saved ".docx" file with mimeType "application/vnd.openxmlformats-officedocument.wordprocessingml.document". But when I access it's endpoint in Spring Content and download it's not exactly Word document, but Zip archive(application/zip). Spring Content 1.2.5 supports ".docx" files, how can we fix it?

Demo project to reproduce issue(.docx file attached): https://github.com/leonaugust/docx-problem

EDIT

Although I understand that resulting file is docx after all and we can choose to open it as Word document, but is there a way to make it less confusing for customers and return back as ".docx" format? In my case a huge amount of documents will most likely be sent in that format

CodePudding user response:

As you have spring-content-rest on the classpath and use the @StoreRestResource annotation I am assuming that you are using that to fetch your content? Please let me know if that is not the case and I will edit.

There are a couple of annotations that, if present on the entity, the spring content rest post/put endpoints would set for you and that are then used later on by the GET endpoint; @MimeType and @OriginalFileName.

If you add these annotations to your entity and set them appropriately in your post endpoint then:-

    @PostMapping
    public UUID create(@RequestParam("file") MultipartFile multipartFile) {
        File file = new File();
        file.setMimeType(multipartFile.getContentType());
        store.setContent(file, multipartFile.getResource());
        file.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        file.setOriginalFileName(multipartFile.getOriginalFilename());
        UUID id = repository.save(file).getId();
        log.info("id {}", id);
        return id;
    }

When your client fetches content (again, I assume) via the Spring Content REST endpoint it will set the following headers:

  • Content-type header
  • content-dispostion form-data attachment header

on the response.

Both of which will direct the browser app as to handle the content appropriately.

This should allow you to make the following get request from your browser.

curl -H 'Accept: application/vnd.openxmlformats-officedocument.wordprocessingml.document' http://localhost:8080/storage/b9ca6fbe-dede-4a51-b444-9e22b798e922

And it should download the attachments as test.docx

Seperately, I'd be curious to know why you added your own "create" endpoint, rather than using the Spring Data REST/Spring Content REST endpoint. It will do this for you automatically. I assume it is because you do not want to use Spring Data REST?

  • Related