从BlobDomain生成的PDF无法在浏览器中打开

时间:2018-10-18 15:59:43

标签: android pdf browser

在这种情况下:调用servlet时,它将pdf文件输出到浏览器中,并且该pdf文件的内容来自数据库返回的BlobDomain对象。在PC中,它似乎可以正常工作。在Android手机中的Firefox中尝试此操作时,它会弹出使用Drive PDF Viewer进行“下载”或“查看”的选项。如果选择下载,则将其下载并可以打开;但如果选择“查看”,它将失败并抱怨格式无效。这是代码段:

try {
            String contentType = "application/pdf";
            String fileExt = ".pdf";
            String disposition = "inline; ";
            String docType = request.getParameter("documentType");
            BlobDomain contentBlob = generateDocument(request, response);

            if (contentBlob == null) {
                return;
            }

            String fileName = "Report";

            // Set to expire far in the past.
            response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
            // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");

            // Set standard HTTP/1.0 public header.
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
            ByteArrayOutputStream out = null;

            try {
                response.setBufferSize(DEFAULT_BUFFER_SIZE);
                response.setContentType(contentType);
                response.addHeader("Content-Disposition", disposition + "filename=" + fileName + fileExt);
                response.setContentLength((int) contentBlob.getLength());

                try (InputStream input = contentBlob.getBinaryStream();
                     OutputStream output = response.getOutputStream()) {

                    // Write file contents to response.
                    byte[] buffer = new byte[(int) contentBlob.getLength()];
                    int length;
                    while ((length = input.read(buffer)) > 0) {
                        output.write(buffer);
                    }
                    output.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                response.flushBuffer();
            }
        } catch (IOException e) {
            e.printStackTrace();            
        }

任何提示都会有所帮助。预先感谢!

0 个答案:

没有答案