我正试图通过他们的驱动API从Google Drive导出一个文档,文档中有example个,但我没有任何运气.
这是我的代码:
@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
public ByteArrayOutputStream downloadSlidesAsPdf(@RequestParam String fileId) throws IOException {
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf").executeMediaAndDownloadTo(outputStream);
return (ByteArrayOutputStream) outputStream;
}
执行此操作将导致控制台中出现警告:
WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.io.ByteArrayOutputStream] with preset Content-Type 'null']
我试着在请求中添加HttpServletResponse
,将内容类型设置为application/pdf
,并将输出流设置为这样:
driveService.files().export(copiedFile.getId(), "application/pdf")
.executeMediaAndDownloadTo(response.getOutputStream());
这是可行的,但它下载的文件没有扩展名,此外,我宁愿它的工作,因为它的设计,而不是与一堆不必要的解决办法.
我还try 了一个中途解决方案,在该解决方案中,我将HttpServletResponse
内容类型设置为application/pdf
,然后在第一次try 时try 返回ByteArrayOutputStream
,但没有成功.