如何从springMVC中的REST响应返回powerpoint(.pptx)文件

时间:2016-08-12 22:32:36

标签: rest spring-mvc

我正在生成一个powerpoint文件(.pptx),我想在REST调用发生时返回此文件。但现在我只能获得.File类型扩展。

@RequestMapping(value = "/ImageManagerPpt/{accessionId}", method = RequestMethod.GET, produces = "application/ppt")
public ResponseEntity<InputStreamResource> createPptforAccessionId(@PathVariable("accessionId") String accessionId,HttpServletResponse response) throws IOException** {
      System.out.println("Creating PPT for Patient Details with id " + accessionId);
      File pptFile = imageManagerService.getPptForAccessionId(accessionId);
      if (pptFile == null) {
          System.out.println("Patient Id with id " + accessionId + " not found");
          return new ResponseEntity<InputStreamResource>(HttpStatus.NOT_FOUND);
      }
      InputStream stream = null;
      try {
          stream = new FileInputStream(pptFile);
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      ClassPathResource classpathfile = new ClassPathResource("Titlelayout3.pptx");
      InputStreamResource inputStreamResource = new InputStreamResource(stream);

      return ResponseEntity.ok().contentLength(classpathfile.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(classpathfile.getInputStream()));
}

-Bharat

1 个答案:

答案 0 :(得分:0)

你试过吗?

InputStream stream = new InputStream(pptFile);   
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();

当您输入InputStream时,您将获得文件。