Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaType for docx file

Tags:

spring-mvc

jsp

I want to display a docx-file on my browser. But I am having problem with the media type for docx-file.

This is my sample code:

@RequestMapping("/view")
public @ResponseBody HttpEntity<byte[]> view(@RequestParam(value = "fileId") int id) throws IOException {

    FileDaoImplement fdi = new FileDaoImplement();
    Files f = fdi.getFile(id);
    byte[] document = f.getByte();

    HttpHeaders header = new HttpHeaders();
    header.setContentType("What should I use to show a docx file??");
    header.set("Content-Disposition", "inline; filename=");
    header.setContentLength(document.length);
    return new HttpEntity<byte[]>(document, header);
}
like image 234
Anando Haider Avatar asked Sep 02 '25 15:09

Anando Haider


1 Answers

you could try with specifying the Mediatype your request produces.

 @RequestMapping(path = "/view", produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")

not sure if you can get it to work with a byte[], but you could use Resource as return-type instead like:

  @GetMapping(path = "/word", produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  public @ResponseBody HttpEntity<Resource>  words() throws IOException {
     return new HttpEntity<Resource>(new ClassPathResource("/static/hello word.docx"));
  }
like image 129
Dirk Deyne Avatar answered Sep 05 '25 16:09

Dirk Deyne