docx文件的MediaType

时间:2018-09-19 15:02:54

标签: spring-mvc jsp

我想在浏览器中显示docx文件。但是我遇到docx文件的媒体类型问题。

这是我的示例代码:

@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);
}

1 个答案:

答案 0 :(得分:0)

您可以尝试指定您的请求产生的Mediatype

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

不确定是否可以使其与byte[]一起使用,但是可以将Resource用作返回类型,例如:

  @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"));
  }