简体   繁体   English

将PDF转换为Base64并将数据存储到数据库的BLOB

[英]Convert PDF to Base64 and store data to BLOB of Database

I want to binary data (eg a PDF) into a BLOB of my Oracle database. 我想将二进制数据(例如PDF)放入我的Oracle数据库的BLOB中。 At first I putted the PDF into a FileInputStream and created a byte-array.Here is the code for that: 起初我将PDF放入FileInputStream并创建了一个byte-array.Here是代码:

public static byte[] createByteArray(File pCurrentFolder, String pNameOfBinaryFile)
    {

        String pathToBinaryData = pCurrentFolder.getAbsolutePath()+"/"+pNameOfBinaryFile;

        File file = new File(pathToBinaryData);
        if (!file.exists())
        {
            System.out.println(pNameOfBinaryFile+" could not be found in folder "+pCurrentFolder.getName());
            return null;
        }

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte fileContent[] = new byte[(int) file.length()];

        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileContent;
    }

I sent this (the byte array) via MyBatis to the database and it worked, so that I had the PDF in my BLOB and I also could read the PDF from my database. 我通过My​​Batis将这个(字节数组)发送到数据库并且它工作,所以我在我的BLOB中有PDF,我也可以从我的数据库中读取PDF。 But now I face the following problem: I have a JDBC Connector for my search engine (FAST ESP...but that dowsnt matter) which connects to a certain database and stores all the content to a xml file. 但是现在我遇到了以下问题:我的搜索引擎有一个JDBC连接器(FAST ESP ......但是那个dowsnt很重要),它连接到某个数据库并将所有内容存储到xml文件中。 Inside this xml file is an element called "data" which contains the binary data inside its CDATA Field. 在这个xml文件中有一个名为“data”的元素,它包含CDATA字段内的二进制数据。

When I want to parse this xml, Java tells me: 当我想解析这个xml时,Java告诉我:

The content of elements must consist of well-formed character data or markup.

With some PDF's i works but with some not. 有一些PDF我的作品,但有些没有。 So I think the problem is, that I have stored them in the database in the wrong way. 所以我认为问题是,我以错误的方式将它们存储在数据库中。

For further information I would reverence to another questions I asked before which is similar to that. 对于进一步的信息,我会尊重我之前提出的另一个类似的问题。

Java: skip binary data in xml file while parsing Java:在解析时跳过xml文件中的二进制数据

Someone there told me that I should encode my PDF (or any binary file) with base64. 有人告诉我,我应该用base64编码我的PDF(或任何二进制文件)。 So that would mean, I do not just put my PDF into a FileInputStream, store the byte[] and put this byte[] to my BLOB of the database. 所以这意味着,我不只是将我的PDF放入FileInputStream,存储byte []并将此byte []放到数据库的BLOB中。 What do I have to do, to store the PDF in correct way inside my database, so that afterwards I can correctly parse my XML file the JDBC connector creates? 我需要做什么,以正确的方式在我的数据库中存储PDF,以便之后我可以正确解析JDBC连接器创建的XML文件?

You can use the JAXB DatatypeConverter class to easily convert your data to base64 without any external dependencies: 您可以使用JAXB DatatypeConverter类轻松地将数据转换为base64,而无需任何外部依赖项:

byte[] arr = YOUR_BINARY_ARRAY;
String result = javax.xml.bind.DatatypeConverter.printBase64Binary(arr);

You can simply add this code to the end of your method and change its return type to a String. 您只需将此代码添加到方法的末尾,并将其返回类型更改为String。

You can try to first convert the bytes to basse64 using Apache Commons as this example: 您可以尝试使用Apache Commons将字节转换为basse64,如下例所示:

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Encode {
    public static void main(String[] args) {
        String hello = "Hello World";


        byte[] encoded = Base64.encodeBase64(hello.getBytes());


        System.out.println(Arrays.toString(encoded));

        String encodedString = new String(encoded);
        System.out.println(hello + " = " + encodedString);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM