Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring/Java显示base64字符串中的PDF_Java_Spring_Image_Pdf_Base64 - Fatal编程技术网

使用Spring/Java显示base64字符串中的PDF

使用Spring/Java显示base64字符串中的PDF,java,spring,image,pdf,base64,Java,Spring,Image,Pdf,Base64,我有一个PDF保存为数据库中的base64CLOB 作为功能测试,我只是想让它显示在我的浏览器中。我在我的控制器中创建了一个新端点,只将base64字符串放入控制器,甚至没有从数据库中获取PDF,如下所示: @RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf") public void makePDF(HttpServletResponse response)

我有一个PDF保存为数据库中的base64CLOB

作为功能测试,我只是想让它显示在我的浏览器中。我在我的控制器中创建了一个新端点,只将base64字符串放入控制器,甚至没有从数据库中获取PDF,如下所示:

@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
    byte[] imageByte = value.getBytes();        
    response.setContentType("application/pdf");
    response.setContentLength(imageBytes.length);
    response.getOutputStream().write(imageBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
试图确切地了解我在这里做什么,以及它为什么起作用。显然,这与Base64解码和使用
文档
对象有关。

堆栈溢出帖子以及为什么

PDF具有基于文本的通用结构。但是,PDF文件可能包含非ASCII(“二进制”)数据,应始终视为二进制文件,-抱歉,无法找到此链接的真相来源

有可能对数据进行有损编码,并在这种情况下对编码的Base-64进行解码

在流式输出之前使用Base64进行解码


因为您发送的不是PDF字节,而是base-64编码的PDF字节。您需要对字符串进行base-64解码,然后发送结果。为什么使用奇怪的存储格式?@JBNizet-我用Base64对其进行了解码,它删除了错误消息,但仍然不显示图像。但至少我走的方向是正确的。谢谢。@Kayaman-这是一个替代品,用于将这些图像作为文件存储在服务器上。我们正在减少文件,只是将这些图像作为CLOB存储在数据库中。为什么要将它们存储为CLOB而不是BLOB?这是最没有意义的存储方式,本可以避免这个问题。谢谢。我确实添加了Base64解码,同时使用了
文档
,并且能够让它工作。更新了我原来的帖子。只是想了解它为什么工作。@aCarella-您提到文件是用Base-64编码保存的,文件必须解码为原始字节集。Base-64不是PDF文件的原始编码。
@RequestMapping(value = "/output.pdf", method = RequestMethod.GET, produces = "application/pdf")
public void makePDF(HttpServletResponse response) throws Exception {
    try {
      String value = "R04jArrrw45jNH6bV02="; //<--This is longer, but I shortened it for this question
      byte[] image = Base64.decodeBase64(value.getBytes());

      Document document = new Document();
      document.setPageSize(PageSize.LETTER);
      PdfWriter.getInstance(document, response.getOutputStream());

      Image labelImage = Image.getInstance(image);
      labelImage.setAlignment(Image.TOP);
      labelImage.scalePercent(new Float("35"));

      document.open();
      document.add(labelImage);

      response.setContentType("application/pdf");
      response.setContentLength(imageBytes.length);
      response.getOutputStream().write(image);

      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
}
Base64.decode(base64String, Base64.NO_WRAP)