亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法通過 Servlet 下載從 BLOB 檢索的 PDF

無法通過 Servlet 下載從 BLOB 檢索的 PDF

慕容森 2021-07-20 21:38:52
我環顧四周并嘗試了一些我看到的解決方案,但似乎沒有任何效果。所以這是我的情況:我有一個 PDF 文件作為 BLOB 存儲在我的 oracle 數據庫中。在我的后端,我調用我的服務以下載該 pdf(在我的實體中,pdf 是 a 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 obj << /Type /Page /Resources << /ProcSet [ /PDF /Text ] /Font 4 0 R /Shading 6 0 R /ExtGState 7 0 R/MediaBox [0 0 595.28 864.00] /Contents 9 0 R /Parent 10 0 R >> endobj但是,這不會下載文件,甚至不會打開它。而且我很確定 pdf 在后端是正確的,因為我在使用時設法得到它:    OutputStream out = new FileOutputStream("Test.pdf");    out.write(pdf);    out.close();我怎樣才能從我的瀏覽器下載 pdf 格式?或者至少打開它?
查看完整描述

1 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

默認情況下,ajax 請求將您的響應視為文本,這會導致非文本文件出現問題。


為了防止這種情況在您的請求中添加一個二進制 responseType,例如


({

  downloadPdf: function() {

    return $http({

      method: 'GET',

      url: this.baseUrl + "/downloadpdf",

      responseType: "blob"

    });

  }

});


查看完整回答
反對 回復 2021-07-29
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號