简体   繁体   中英

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. At first I putted the PDF into a FileInputStream and created a byte-array.Here is the code for that:

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. 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. Inside this xml file is an element called "data" which contains the binary data inside its CDATA Field.

When I want to parse this xml, Java tells me:

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

With some PDF's i works but with some not. 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

Someone there told me that I should encode my PDF (or any binary file) with base64. 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. 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?

You can use the JAXB DatatypeConverter class to easily convert your data to base64 without any external dependencies:

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.

You can try to first convert the bytes to basse64 using Apache Commons as this example:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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