在我的WebService中,我有一个应该为客户端下载PDF文件的服务(GET Http请求),但它全部都是空白的。
我的控制器:
InputStream is = this.getClass().getResourceAsStream(nomeArquivo);
ByteArrayOutputStream pdfBA = termoService.readFully(is);
httpServletResponse.addHeader(HttpHeaders.CONTENT_TYPE, Constants.HTTP_CONTENT_TYPE_APPLICATION_PDF);
httpServletResponse.addHeader("Content-Transfer-Encoding", "binary");
httpServletResponse.addHeader("Content-Disposition","attachment; filename=\"termo-adesao.pdf\"");
httpServletResponse.setContentLength(pdfBA.size());
OutputStream os = httpServletResponse.getOutputStream();
pdfBA.writeTo(os);
os.flush();
MyService.readFully:
public static ByteArrayOutputStream readFully(InputStream stream) throws IOException {
byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1){
baos.write(buffer, 0, bytesRead);
}
return baos;
}
我在互联网上看到了一些教程,但不知何故,每当我拿到文件时,它会打开与原始页面相同的页数,但它都是空白的(字母缺少,而不是白色)。在stackoverflow中,我看到了一些类似的问题,但都与JSF有关。