使用spring和java在浏览器中打开PDF



下面我正在阅读来自数据库的PDF…我正在尝试在浏览器中打开PDF文件…但不是打开浏览器,它总是下载…代码如下,请帮我在浏览器中打开PDF,而不是要求下载....

     @RequestMapping(value = "account/documents/{id}", method = RequestMethod.GET)
public void downloadDocument(HttpServletRequest request,
        HttpServletResponse response, @PathVariable("id") String docId)
        throws Exception {
    HttpSession session = request.getSession(true);
    int accountId = (Integer) session.getAttribute("ownerAccountId");
    Map<String, String> docMap = DbInteractor.getUploadedDocsByDocId(
            Integer.valueOf(docId), accountId);
    String docName = docMap.get("name");
    String typeName = docMap.get("type");
    String[] fileName = docName.split("\.(?=[^\.]+$)");
    typeName = typeName.replace(" ", "");
    if (typeName.equals("CCD/CCR")) {
        typeName = "CCDorCCR";
    }
    String filename = typeName + docId + "." + fileName[1];
    System.out.println(filename);
    FileInputStream fileInputStream = new FileInputStream(
            Constants.DOCUMENTS_PATH + filename);
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control",
            "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Content-disposition", "inline; filename="
            + docName);
    OutputStream os = response.getOutputStream();
    IOUtils.copy(fileInputStream, os);
    os.flush();
    os.close();
}

你能帮帮我吗.....

您需要更改内容类型,以便浏览器能够识别数据。在代码中添加这一行-

response.setContentType("application/pdf");

此外,您可以尝试使用response.setHeader("Content-Disposition", "inline");带或不带附件属性。因为我不确定,也没有检查过。: -)

我删除了:

response.setHeader("Content-disposition", "inline; filename="+ docName);

和补充道:

response.setContentType("application/pdf");

这是我使用ResponseEntity<byte[]>的解决方案

@RequestMapping(value = "/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getExport() {
    ITextRenderer renderer = new ITextRenderer();
    ByteArrayOutputStream boas = null;
    try {
        String inputFile = "files/templates/autodebit.html";
        String outputFile = "files/generated/autodebit_"+filenameDate.format(new Date())+".pdf";
        String html = new String(Files.readAllBytes(Paths.get(inputFile)));
        final Document document = Jsoup.parse(html);
        document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
        document.body().select(".DOC_GENERATED_DATE").html(readableDate.format(new Date()));
        renderer.setDocumentFromString(document.html());
        renderer.layout();
        try (OutputStream os = Files.newOutputStream(Paths.get(outputFile))) {
            renderer.createPDF(os);
            os.close();
            PdfReader reader = new PdfReader(outputFile);
            boas = new ByteArrayOutputStream();
            PdfStamper stamper = new PdfStamper(reader, boas);
            stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1); 
            stamper.close();
        } catch (DocumentException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   catch (IOException ex) {
        Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_PDF);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<>(boas.toByteArray(), headers, HttpStatus.OK);
    return response;
}

希望有帮助!

相关内容

  • 没有找到相关文章

最新更新