代码之家  ›  专栏  ›  技术社区  ›  Ellone

无法通过Servlet下载从BLOB检索到的PDF

  •  0
  • Ellone  · 技术社区  · 6 年前

    我环顾四周,尝试了一些我看到的解决方案,但似乎什么也没用。

    我有一个PDF文件作为BLOB存储在我的oracle数据库中。 在我的后端,我调用我的服务以下载该pdf(在我的实体中,pdf是 byte[]

    @GET
    @Path("/downloadpdf")
    @Produces("application/pdf")
    public HttpServletResponse downloadPdf(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            UserGuideJpaService userGuideService = new UserGuideJpaServiceImpl();
            HashMap<String, Object> result = userGuideService.getPdfGuide();
            if ("0".equals(result.get("returnCode"))) {
                UserGuide userGuide = (userGuide) result.get("userGuide");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-control", "private");
                response.setDateHeader("Expires", 0);
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition", "attachment; filename=\"APP - User Guide.pdf\"");
    
                byte[] pdf = userGuide.getPdf();
    
                if (pdf != null) {
                    response.setContentLength(pdf.length);
                    ServletOutputStream out = response.getOutputStream();
                    out.write(pdf);
                    out.flush();
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    

    回到我的前端,我有这个:

    注: 这就是我的 response.data

    %PDF-1.4%

    8 0对象<</Type/Page/Resources<</ProcSet[/PDF/Text]/Font 4 0 R/阴影6 0 R/ExtGState 7 0 R

    /MediaBox[0 0 595.28 864.00]/内容9 0 R/父级10 0 R>>endobj

    xXn7号?]84aw=IJrv%@Qas+r%E-%{x| 1oo{jbCN_{]9rXUeT]WVmq5 |\5aè“T0n~”OX9 #~+b579)R/f5xf5wwell}A.'7Eji号 ]\^+fUT<1,ZrE3s4bzU,f4层 U*7vN4“^Nf”!~2\G+,b<2/WIUV2%b{ZdHR.G%@0Ln)9。。。

    以及javascript方面:

    $scope.downloadPdf = function() {
      APIClientService.downloadPdf().then(function(response) {
        // #1
        var URL, blob, downloadLink, downloadUrl;
        downloadLink = document.createElement('a');
        downloadLink.target = '_blank';
        downloadLink.download = 'APP - User Guide.pdf';
        blob = new Blob([response.data], {
          type: 'application/pdf'
        });
        URL = window.URL || window.webkitURL;
        downloadUrl = URL.createObjectURL(blob);
        downloadLink.href = downloadUrl;
        document.body.append(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);
        URL.revokeObjectURL(downloadUrl);
        // # 2
        window.open("data:application/pdf," + encodeURI(response.data));
        // # 3
        window.open("data:application/pdf," + escape(response.data));
      });
    };
    

    而且我非常确定pdf在后端是正确的,因为我在使用以下文件时成功地获得了它:

        OutputStream out = new FileOutputStream("Test.pdf");
        out.write(pdf);
        out.close();
    

    如何从浏览器下载pdf格式?或者至少打开它?

    编辑:这是来自APIClientService的方法:

    ({
      downloadPdf: function() {
        return $http({
          method: 'GET',
          url: this.baseUrl + "/downloadpdf"
        });
      }
    });
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Musa    6 年前

    默认情况下,ajax请求将响应视为文本,这将导致非文本文件出现问题。

    ({
      downloadPdf: function() {
        return $http({
          method: 'GET',
          url: this.baseUrl + "/downloadpdf",
          responseType: "blob"
        });
      }
    });